From 488e42d77b40444222f36a9c02205613d1097bb6 Mon Sep 17 00:00:00 2001 From: bbugh Date: Fri, 19 Oct 2012 11:24:08 -0700 Subject: [PATCH] Modified define_list_strategy_method to accept and use a block. --- .../strategy_syntax_method_registrar.rb | 4 ++-- spec/acceptance/build_list_spec.rb | 17 ++++++++++++++++- spec/acceptance/create_list_spec.rb | 17 ++++++++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/factory_girl/strategy_syntax_method_registrar.rb b/lib/factory_girl/strategy_syntax_method_registrar.rb index b0e0c17e2..ab752d463 100644 --- a/lib/factory_girl/strategy_syntax_method_registrar.rb +++ b/lib/factory_girl/strategy_syntax_method_registrar.rb @@ -23,8 +23,8 @@ def define_singular_strategy_method def define_list_strategy_method strategy_name = @strategy_name - define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides| - amount.times.map { send(strategy_name, name, *traits_and_overrides) } + define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides, &block| + amount.times.map { send(strategy_name, name, *traits_and_overrides, &block) } end end diff --git a/spec/acceptance/build_list_spec.rb b/spec/acceptance/build_list_spec.rb index 2b42c0a4e..9e50403b4 100644 --- a/spec/acceptance/build_list_spec.rb +++ b/spec/acceptance/build_list_spec.rb @@ -2,11 +2,12 @@ describe "build multiple instances" do before do - define_model('Post', title: :string) + define_model('Post', title: :string, position: :integer) FactoryGirl.define do factory(:post) do |post| post.title "Through the Looking Glass" + post.position { rand(10**4) } end end end @@ -38,4 +39,18 @@ end end end + + context "with a block" do + subject do + FactoryGirl.build_list(:post, 20, title: "The Listing of the Block") do |post| + post.position = post.id + end + end + + it "correctly uses the set value" do + subject.each_with_index do |record, index| + record.position.should == record.id + end + end + end end diff --git a/spec/acceptance/create_list_spec.rb b/spec/acceptance/create_list_spec.rb index ea57fddea..701ef531e 100644 --- a/spec/acceptance/create_list_spec.rb +++ b/spec/acceptance/create_list_spec.rb @@ -2,11 +2,12 @@ describe "create multiple instances" do before do - define_model('Post', title: :string) + define_model('Post', title: :string, position: :integer) FactoryGirl.define do factory(:post) do |post| post.title "Through the Looking Glass" + post.position { rand(10**4) } end end end @@ -38,6 +39,20 @@ end end end + + context "with a block" do + subject do + FactoryGirl.create_list(:post, 20, title: "The Listing of the Block") do |post| + post.position = post.id + end + end + + it "uses the new values" do + subject.each_with_index do |record, index| + record.position.should == record.id + end + end + end end describe "multiple creates and ignored attributes to dynamically build attribute lists" do