Skip to content
This repository
Browse code

Modified define_list_strategy_method to accept and use a block.

  • Loading branch information...
commit 488e42d77b40444222f36a9c02205613d1097bb6 1 parent 06ff258
bbugh authored October 19, 2012 joshuaclayton committed October 22, 2012
4  lib/factory_girl/strategy_syntax_method_registrar.rb
@@ -23,8 +23,8 @@ def define_singular_strategy_method
23 23
     def define_list_strategy_method
24 24
       strategy_name = @strategy_name
25 25
 
26  
-      define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides|
27  
-        amount.times.map { send(strategy_name, name, *traits_and_overrides) }
  26
+      define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides, &block|
  27
+        amount.times.map { send(strategy_name, name, *traits_and_overrides, &block) }
28 28
       end
29 29
     end
30 30
 
17  spec/acceptance/build_list_spec.rb
@@ -2,11 +2,12 @@
2 2
 
3 3
 describe "build multiple instances" do
4 4
   before do
5  
-    define_model('Post', title: :string)
  5
+    define_model('Post', title: :string, position: :integer)
6 6
 
7 7
     FactoryGirl.define do
8 8
       factory(:post) do |post|
9 9
         post.title "Through the Looking Glass"
  10
+        post.position { rand(10**4) }
10 11
       end
11 12
     end
12 13
   end
@@ -38,4 +39,18 @@
38 39
       end
39 40
     end
40 41
   end
  42
+
  43
+  context "with a block" do
  44
+    subject do
  45
+      FactoryGirl.build_list(:post, 20, title: "The Listing of the Block") do |post|
  46
+        post.position = post.id
  47
+      end
  48
+    end
  49
+
  50
+    it "correctly uses the set value" do
  51
+      subject.each_with_index do |record, index|
  52
+        record.position.should == record.id
  53
+      end
  54
+    end
  55
+  end
41 56
 end
17  spec/acceptance/create_list_spec.rb
@@ -2,11 +2,12 @@
2 2
 
3 3
 describe "create multiple instances" do
4 4
   before do
5  
-    define_model('Post', title: :string)
  5
+    define_model('Post', title: :string, position: :integer)
6 6
 
7 7
     FactoryGirl.define do
8 8
       factory(:post) do |post|
9 9
         post.title "Through the Looking Glass"
  10
+        post.position { rand(10**4) }
10 11
       end
11 12
     end
12 13
   end
@@ -38,6 +39,20 @@
38 39
       end
39 40
     end
40 41
   end
  42
+
  43
+  context "with a block" do
  44
+    subject do
  45
+      FactoryGirl.create_list(:post, 20, title: "The Listing of the Block") do |post|
  46
+        post.position = post.id
  47
+      end
  48
+    end
  49
+
  50
+    it "uses the new values" do
  51
+      subject.each_with_index do |record, index|
  52
+        record.position.should == record.id
  53
+      end
  54
+    end
  55
+  end
41 56
 end
42 57
 
43 58
 describe "multiple creates and ignored attributes to dynamically build attribute lists" do

0 notes on commit 488e42d

Please sign in to comment.
Something went wrong with that request. Please try again.