File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,6 +60,22 @@ describe Avram::Factory do
6060 end
6161 end
6262
63+ describe " create_many" do
64+ it " creates N factories" do
65+ tags = TagFactory .create_many(4 )
66+ tags.size.should eq(4 )
67+ tags.should be_a(Array (Tag ))
68+ end
69+
70+ it " takes a block to customize the factories" do
71+ tags = TagFactory .create_many(4 ) do |factory |
72+ factory.name(factory.sequence(" tag-me" ))
73+ end
74+ tags.size.should eq(4 )
75+ tags.map(& .name).should eq([" tag-me-1" , " tag-me-2" , " tag-me-3" , " tag-me-4" ])
76+ end
77+ end
78+
6379 describe " before_save" do
6480 it " sets the association before saving" do
6581 factory = ScanFactory .new
Original file line number Diff line number Diff line change @@ -85,6 +85,37 @@ abstract class Avram::Factory
8585 run_after_save_callbacks(record)
8686 end
8787
88+ # Returns an array with `number` instances of the model from the Factory.
89+ #
90+ # Usage:
91+ #
92+ # ```
93+ # tags = TagFactory.create_many(2)
94+ # typeof(tags) # => Array(Tag)
95+ # tags.size # => 2
96+ # ```
97+ def self.create_many (number : Int32 )
98+ create_many(number) { |factory | factory }
99+ end
100+
101+ # Similar to `create_many(n)`, but accepts a block which yields the factory instance.
102+ #
103+ # All factories receive the same argument values.
104+ #
105+ # Usage:
106+ #
107+ # ```
108+ # TagFactory.create_many(2) do |factory|
109+ # # set both factories name to "test"
110+ # factory.name("test")
111+ # end
112+ # ```
113+ def self.create_many (number : Int32 , & )
114+ (1 ..number).to_a.map do |_ |
115+ self .create { |factory | yield (factory) }
116+ end
117+ end
118+
88119 # Returns an array with 2 instances of the model from the Factory.
89120 #
90121 # Usage:
@@ -95,7 +126,7 @@ abstract class Avram::Factory
95126 # tags.size # => 2
96127 # ```
97128 def self.create_pair
98- create_pair { | factory | factory }
129+ create_many( 2 )
99130 end
100131
101132 # Similar to `create_pair`, but accepts a block which yields the factory instance.
@@ -111,8 +142,8 @@ abstract class Avram::Factory
111142 # end
112143 # ```
113144 def self.create_pair (& )
114- [ 1 , 2 ].map do |_ |
115- self .create { | factory | yield (factory) }
145+ create_many( 2 ) do |factory |
146+ yield factory
116147 end
117148 end
118149
You can’t perform that action at this time.
0 commit comments