Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modified define_list_strategy_method to accept and use a block.

  • Loading branch information...
commit 488e42d77b40444222f36a9c02205613d1097bb6 1 parent 06ff258
bbugh bbugh authored joshuaclayton committed
4 lib/factory_girl/strategy_syntax_method_registrar.rb
View
@@ -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
17 spec/acceptance/build_list_spec.rb
View
@@ -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
17 spec/acceptance/create_list_spec.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.