Skip to content

Commit

Permalink
Modified define_list_strategy_method to accept and use a block.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbugh authored and joshuaclayton committed Oct 22, 2012
1 parent 06ff258 commit 488e42d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/factory_girl/strategy_syntax_method_registrar.rb
Expand Up @@ -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

Expand Down
17 changes: 16 additions & 1 deletion spec/acceptance/build_list_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
17 changes: 16 additions & 1 deletion spec/acceptance/create_list_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 488e42d

Please sign in to comment.