gabrielg-factory_girl 1.1.9 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +55 -0
- data/lib/factory_girl/factory.rb +2 -2
- data/test/factory_test.rb +28 -31
- metadata +1 -1
data/README.textile
CHANGED
@@ -117,6 +117,19 @@ end</code></pre>
|
|
117
117
|
If the factory name is the same as the association name, the factory name can
|
118
118
|
be left out.
|
119
119
|
|
120
|
+
h2. Multiple associations
|
121
|
+
|
122
|
+
If a factory specifies a :count on an association definition, it's considered
|
123
|
+
to be a collection of objects. For example:
|
124
|
+
|
125
|
+
<pre><code># This will create 2 associated posts on the author
|
126
|
+
Factory.define :post do |p|
|
127
|
+
p.title "Hello world"
|
128
|
+
end
|
129
|
+
|
130
|
+
Factory.define :author do |a|
|
131
|
+
a.association :posts, :count => 2
|
132
|
+
end</code></pre>
|
120
133
|
|
121
134
|
h2. Sequences
|
122
135
|
|
@@ -135,6 +148,48 @@ Factory.next :email
|
|
135
148
|
Factory.next :email
|
136
149
|
# => "person2@example.com"</code></pre>
|
137
150
|
|
151
|
+
h2. Callbacks
|
152
|
+
|
153
|
+
Callbacks can be specified after build and after create. If the block arity is
|
154
|
+
greater than 1, the callback block will receive the instance of the created/built
|
155
|
+
object, and the attributes used to build it. If the block arity is 1 the callback
|
156
|
+
will just receive the instance of the object. For example:
|
157
|
+
|
158
|
+
<pre><code>
|
159
|
+
Factory.define :post do |p|
|
160
|
+
p.author "joe"
|
161
|
+
p.after_build do |post, attrs|
|
162
|
+
# post will be an instance of Post
|
163
|
+
# attrs will be {:author => "joe"}
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
Factory.define :user do |u|
|
168
|
+
u.name "john"
|
169
|
+
p.after_create do |user|
|
170
|
+
# user will be an instance of User
|
171
|
+
end
|
172
|
+
end</code></pre>
|
173
|
+
|
174
|
+
h2. Inheritance
|
175
|
+
|
176
|
+
Factories can be inherited from each other by specifying the :inherit option when
|
177
|
+
defining a factory. For example:
|
178
|
+
|
179
|
+
<pre><code>
|
180
|
+
Factory.define :user do |u|
|
181
|
+
u.name "John Doe"
|
182
|
+
u.role "user"
|
183
|
+
end
|
184
|
+
|
185
|
+
Factory.define :admin_user, :inherit => :user do |u|
|
186
|
+
u.role "admin"
|
187
|
+
end
|
188
|
+
|
189
|
+
user = Factory(:admin_user)
|
190
|
+
user.name # => "John Doe"
|
191
|
+
user.role # => "admin"
|
192
|
+
</code></pre>
|
138
193
|
|
139
194
|
h2. Using factories
|
140
195
|
|
data/lib/factory_girl/factory.rb
CHANGED
@@ -139,7 +139,7 @@ class Factory
|
|
139
139
|
instance.save!
|
140
140
|
if callback = @callbacks[:after_create]
|
141
141
|
params = (callback.arity > 1) ? [instance, final_attrs] : [instance]
|
142
|
-
|
142
|
+
callback.call(*params)
|
143
143
|
end
|
144
144
|
instance
|
145
145
|
end
|
@@ -263,7 +263,7 @@ class Factory
|
|
263
263
|
end
|
264
264
|
if callback = @callbacks[:after_build]
|
265
265
|
params = (callback.arity > 1) ? [instance, attrs] : [instance]
|
266
|
-
|
266
|
+
callback.call(*params)
|
267
267
|
end
|
268
268
|
[instance, attrs]
|
269
269
|
end
|
data/test/factory_test.rb
CHANGED
@@ -437,46 +437,44 @@ class FactoryTest < Test::Unit::TestCase
|
|
437
437
|
end
|
438
438
|
|
439
439
|
context "and an after_build callback has been registered" do
|
440
|
-
saved = nil
|
441
|
-
setup do
|
442
|
-
@the_block = lambda {|user| assert user.new_record?; saved = user}
|
443
440
|
|
441
|
+
setup do
|
442
|
+
@the_block = lambda {|user| assert user.new_record?; @saved = user}
|
444
443
|
@factory.after_build &@the_block
|
445
444
|
end
|
446
445
|
|
447
446
|
should "call the callback when the object is built" do
|
448
|
-
@
|
447
|
+
@the_block.expects(:call)
|
449
448
|
@factory.build
|
450
449
|
end
|
451
450
|
|
452
451
|
should "call the callback when the object is created" do
|
453
|
-
@
|
452
|
+
@the_block.expects(:call)
|
454
453
|
@factory.create
|
455
454
|
end
|
456
455
|
|
457
456
|
should "yield the instance to block if block arity is <= 1" do
|
458
|
-
@
|
459
|
-
@factory.expects(:instance_exec).with(anything)
|
457
|
+
@the_block.expects(:call).with(anything)
|
460
458
|
@factory.build
|
461
459
|
end
|
462
460
|
|
463
461
|
should "yield the instance and attrs to block if block arity is > 1" do
|
464
|
-
|
465
|
-
@factory.
|
462
|
+
the_block = lambda {|user,attrs|}
|
463
|
+
@factory.after_build &the_block
|
464
|
+
the_block.expects(:call).with(anything, instance_of(Hash))
|
466
465
|
@factory.build
|
467
466
|
end
|
468
467
|
|
469
468
|
should "yield the instance to the callback when called" do
|
470
469
|
instance = @factory.build
|
471
|
-
assert_equal saved, instance
|
470
|
+
assert_equal @saved, instance
|
472
471
|
end
|
473
472
|
end
|
474
473
|
|
475
474
|
context "and an after_create callback has been registered" do
|
476
|
-
saved = nil
|
477
|
-
setup do
|
478
|
-
@the_block = lambda {|user| assert !user.new_record?; saved = user}
|
479
475
|
|
476
|
+
setup do
|
477
|
+
@the_block = lambda {|user| assert !user.new_record?; @saved = user}
|
480
478
|
@factory.after_create &@the_block
|
481
479
|
end
|
482
480
|
|
@@ -486,61 +484,60 @@ class FactoryTest < Test::Unit::TestCase
|
|
486
484
|
end
|
487
485
|
|
488
486
|
should "call the callback when the object is created" do
|
489
|
-
@
|
487
|
+
@the_block.expects(:call).with(anything)
|
490
488
|
@factory.create
|
491
489
|
end
|
492
490
|
|
493
491
|
should "yield the instance to block if block arity is <= 1" do
|
494
|
-
@
|
495
|
-
@factory.expects(:instance_exec).with(anything)
|
492
|
+
@the_block.expects(:call).with(anything)
|
496
493
|
@factory.create
|
497
494
|
end
|
498
495
|
|
499
496
|
should "yield the instance and attrs to block if block arity is > 1" do
|
500
|
-
|
501
|
-
@factory.
|
497
|
+
the_block = lambda {|user,attrs|}
|
498
|
+
@factory.after_create &the_block
|
499
|
+
the_block.expects(:call).with(anything, instance_of(Hash))
|
502
500
|
@factory.create
|
503
501
|
end
|
504
502
|
|
505
503
|
should "yield the instance to the callback when called" do
|
506
504
|
instance = @factory.create
|
507
|
-
assert_equal saved, instance
|
505
|
+
assert_equal @saved, instance
|
508
506
|
end
|
509
507
|
end
|
510
508
|
|
511
509
|
context "and both after_build and after_create callbacks have been registered" do
|
512
|
-
|
510
|
+
|
513
511
|
setup do
|
514
|
-
|
515
|
-
@
|
516
|
-
@the_after_create_block = lambda {|user| test.assert(!user.new_record?); post_create = user}
|
512
|
+
@the_after_build_block = lambda {|user| assert(user.new_record?); @post_build = user}
|
513
|
+
@the_after_create_block = lambda {|user| assert(!user.new_record?); @post_create = user}
|
517
514
|
|
518
515
|
@factory.after_build &@the_after_build_block
|
519
516
|
@factory.after_create &@the_after_create_block
|
520
517
|
end
|
521
518
|
|
522
519
|
should "only call the after_build callback when the object is built" do
|
523
|
-
@
|
520
|
+
@the_after_build_block.expects(:call).once
|
524
521
|
@factory.build
|
525
522
|
end
|
526
523
|
|
527
524
|
should "call both callbacks when the object is created" do
|
528
|
-
@
|
525
|
+
@the_after_build_block.expects(:call).once
|
526
|
+
@the_after_create_block.expects(:call).once
|
529
527
|
@factory.create
|
530
528
|
end
|
531
529
|
|
532
530
|
should "yield the same instance to each callback when called" do
|
533
531
|
instance = @factory.create
|
534
|
-
assert_equal post_build, instance
|
535
|
-
assert_equal post_create, instance
|
536
|
-
assert_equal post_create, post_build
|
532
|
+
assert_equal @post_build, instance
|
533
|
+
assert_equal @post_create, instance
|
534
|
+
assert_equal @post_create, @post_build
|
537
535
|
end
|
538
536
|
|
539
537
|
should "call the after_build callback before the after_create callback when objects are created" do
|
540
538
|
# TODO - is this good enough to detect "beforeness"?
|
541
|
-
|
542
|
-
@
|
543
|
-
@the_after_create_block = lambda {|user| test.assert_not_nil @post_build; test.assert_nil @post_create; @post_create = user}
|
539
|
+
@the_after_build_block = lambda {|user| assert_nil @post_build; assert_nil @post_create; @post_build = user}
|
540
|
+
@the_after_create_block = lambda {|user| assert_not_nil @post_build; assert_nil @post_create; @post_create = user}
|
544
541
|
|
545
542
|
@factory.after_build &@the_after_build_block
|
546
543
|
@factory.after_create &@the_after_create_block
|