Modified define_list_strategy_method to accept and use a block.

This commit is contained in:
bbugh 2012-10-19 11:24:08 -07:00 committed by Joshua Clayton
parent 06ff2587d2
commit 488e42d77b
3 changed files with 34 additions and 4 deletions

View File

@ -23,8 +23,8 @@ module FactoryGirl
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

View File

@ -2,11 +2,12 @@ require 'spec_helper'
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 @@ describe "build multiple instances" do
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

View File

@ -2,11 +2,12 @@ require 'spec_helper'
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 @@ describe "create multiple instances" do
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