factory_bot 4.10.0 → 4.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/GETTING_STARTED.md +92 -95
- data/NEWS +5 -0
- data/README.md +10 -2
- data/lib/factory_bot/definition_proxy.rb +27 -0
- data/lib/factory_bot/strategy/stub.rb +2 -7
- data/lib/factory_bot/version.rb +1 -1
- metadata +41 -69
- data/.autotest +0 -9
- data/.gitignore +0 -10
- data/.rspec +0 -3
- data/.simplecov +0 -4
- data/.travis.yml +0 -58
- data/.yardopts +0 -5
- data/Appraisals +0 -23
- data/Gemfile +0 -8
- data/Gemfile.lock +0 -103
- data/Rakefile +0 -36
- data/cucumber.yml +0 -1
- data/factory_bot.gemspec +0 -36
- data/factory_girl.gemspec +0 -40
- data/gemfiles/3.2.gemfile +0 -10
- data/gemfiles/3.2.gemfile.lock +0 -107
- data/gemfiles/4.0.gemfile +0 -10
- data/gemfiles/4.0.gemfile.lock +0 -107
- data/gemfiles/4.1.gemfile +0 -10
- data/gemfiles/4.1.gemfile.lock +0 -106
- data/gemfiles/4.2.gemfile +0 -10
- data/gemfiles/4.2.gemfile.lock +0 -106
- data/gemfiles/5.0.gemfile +0 -10
- data/gemfiles/5.0.gemfile.lock +0 -104
- data/gemfiles/5.1.gemfile +0 -10
- data/gemfiles/5.1.gemfile.lock +0 -104
- data/lib/factory_girl.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89e05780d4f57018d6825a36765bae4eafd20a5ea7ef0dd087f056a467d7c04a
|
4
|
+
data.tar.gz: 57af145df0452c560e27c979f3f76a1ee680e6e0e7f10dbb6af253a02943f942
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bcda07de6b55b94f5ab976c058520adf4340cd6ca4518b06ceda03dc2a76ec190a0ef7519d3273440868b2f2f772f0b56f9c9aa71a990db6c32986b1a4f97c4
|
7
|
+
data.tar.gz: 57506e51449c9b2516862498a3078e79c4426ff441e96ee46577b83dbafc97bb91b610b176d1d7b9167b722515954cafa2816c98b866ac8b8586cdbe73273e54
|
data/GETTING_STARTED.md
CHANGED
@@ -30,13 +30,17 @@ Configure your test suite
|
|
30
30
|
|
31
31
|
### RSpec
|
32
32
|
|
33
|
+
If you're using Rails:
|
34
|
+
|
33
35
|
```ruby
|
34
|
-
# spec/support/factory_bot.rb
|
35
36
|
RSpec.configure do |config|
|
36
37
|
config.include FactoryBot::Syntax::Methods
|
37
38
|
end
|
39
|
+
```
|
38
40
|
|
39
|
-
|
41
|
+
If you're *not* using Rails:
|
42
|
+
|
43
|
+
```ruby
|
40
44
|
RSpec.configure do |config|
|
41
45
|
config.include FactoryBot::Syntax::Methods
|
42
46
|
|
@@ -46,12 +50,6 @@ RSpec.configure do |config|
|
|
46
50
|
end
|
47
51
|
```
|
48
52
|
|
49
|
-
Remember to require the above file in your rails_helper since the support folder isn't eagerly loaded
|
50
|
-
|
51
|
-
```ruby
|
52
|
-
require 'support/factory_bot'
|
53
|
-
```
|
54
|
-
|
55
53
|
### Test::Unit
|
56
54
|
|
57
55
|
```ruby
|
@@ -110,20 +108,29 @@ Each factory has a name and a set of attributes. The name is used to guess the c
|
|
110
108
|
# This will guess the User class
|
111
109
|
FactoryBot.define do
|
112
110
|
factory :user do
|
113
|
-
first_name "John"
|
114
|
-
last_name "Doe"
|
115
|
-
admin false
|
111
|
+
first_name { "John" }
|
112
|
+
last_name { "Doe" }
|
113
|
+
admin { false }
|
116
114
|
end
|
117
115
|
|
118
116
|
# This will use the User class (Admin would have been guessed)
|
119
117
|
factory :admin, class: User do
|
120
|
-
first_name "Admin"
|
121
|
-
last_name
|
122
|
-
admin
|
118
|
+
first_name { "Admin" }
|
119
|
+
last_name { "User" }
|
120
|
+
admin { true }
|
123
121
|
end
|
124
122
|
end
|
125
123
|
```
|
126
124
|
|
125
|
+
Because of the block syntax in Ruby, defining attributes as `Hash`es (for
|
126
|
+
serialized/JSON columns, for example) requires two sets of curly brackets:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
factory :program do
|
130
|
+
configuration { { auto_resolve: false, auto_define: true } }
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
127
134
|
It is highly recommended that you have one factory for each class that provides the simplest set of attributes necessary to create an instance of that class. If you're creating ActiveRecord objects, that means that you should only provide attributes that are required through validations and that do not have defaults. Other factories can be created through inheritance to cover common scenarios for each class.
|
128
135
|
|
129
136
|
Attempting to define multiple factories with the same name will raise an error.
|
@@ -170,40 +177,30 @@ user.first_name
|
|
170
177
|
# => "Joe"
|
171
178
|
```
|
172
179
|
|
173
|
-
|
180
|
+
Static Attributes
|
174
181
|
------------------
|
175
182
|
|
176
|
-
|
177
|
-
|
178
|
-
attributes that must be dynamically generated) will need values assigned each
|
179
|
-
time an instance is generated. These "dynamic" attributes can be added by passing a
|
180
|
-
block instead of a parameter:
|
183
|
+
Static attributes, without a block, are deprecated and will be removed in
|
184
|
+
factory\_bot 5.
|
181
185
|
|
182
186
|
```ruby
|
183
187
|
factory :user do
|
184
|
-
#
|
185
|
-
|
186
|
-
date_of_birth { 21.years.ago }
|
187
|
-
end
|
188
|
-
```
|
188
|
+
# Do not use deprecated static attributes
|
189
|
+
admin true
|
189
190
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
```ruby
|
194
|
-
factory :program do
|
195
|
-
configuration { { auto_resolve: false, auto_define: true } }
|
191
|
+
# Use dynamic attribues instead
|
192
|
+
admin { true }
|
196
193
|
end
|
197
194
|
```
|
198
195
|
|
199
196
|
Aliases
|
200
197
|
-------
|
201
|
-
factory_bot allows you to define aliases to existing factories to make them easier to re-use. This could come in handy when, for example, your Post object has an author attribute that actually refers to an instance of a User class. While normally factory_bot can infer the factory name from the association name, in this case it will look for
|
198
|
+
factory_bot allows you to define aliases to existing factories to make them easier to re-use. This could come in handy when, for example, your Post object has an author attribute that actually refers to an instance of a User class. While normally factory_bot can infer the factory name from the association name, in this case it will look for an author factory in vain. So, alias your user factory so it can be used under alias names.
|
202
199
|
|
203
200
|
```ruby
|
204
201
|
factory :user, aliases: [:author, :commenter] do
|
205
|
-
first_name
|
206
|
-
last_name
|
202
|
+
first_name { "John" }
|
203
|
+
last_name { "Doe" }
|
207
204
|
date_of_birth { 18.years.ago }
|
208
205
|
end
|
209
206
|
|
@@ -211,15 +208,15 @@ factory :post do
|
|
211
208
|
author
|
212
209
|
# instead of
|
213
210
|
# association :author, factory: :user
|
214
|
-
title "How to read a book effectively"
|
215
|
-
body
|
211
|
+
title { "How to read a book effectively" }
|
212
|
+
body { "There are five steps involved." }
|
216
213
|
end
|
217
214
|
|
218
215
|
factory :comment do
|
219
216
|
commenter
|
220
217
|
# instead of
|
221
218
|
# association :commenter, factory: :user
|
222
|
-
body "Great article!"
|
219
|
+
body { "Great article!" }
|
223
220
|
end
|
224
221
|
```
|
225
222
|
|
@@ -231,8 +228,8 @@ that is yielded to dynamic attribute blocks:
|
|
231
228
|
|
232
229
|
```ruby
|
233
230
|
factory :user do
|
234
|
-
first_name "Joe"
|
235
|
-
last_name "Blow"
|
231
|
+
first_name { "Joe" }
|
232
|
+
last_name { "Blow" }
|
236
233
|
email { "#{first_name}.#{last_name}@example.com".downcase }
|
237
234
|
end
|
238
235
|
|
@@ -248,11 +245,11 @@ There may be times where your code can be DRYed up by passing in transient attri
|
|
248
245
|
```ruby
|
249
246
|
factory :user do
|
250
247
|
transient do
|
251
|
-
rockstar true
|
252
|
-
upcased
|
248
|
+
rockstar { true }
|
249
|
+
upcased { false }
|
253
250
|
end
|
254
251
|
|
255
|
-
name
|
252
|
+
name { "John Doe#{" - Rockstar" if rockstar}" }
|
256
253
|
email { "#{name.downcase}@example.com" }
|
257
254
|
|
258
255
|
after(:create) do |user, evaluator|
|
@@ -296,10 +293,10 @@ You can easily create multiple factories for the same class without repeating co
|
|
296
293
|
|
297
294
|
```ruby
|
298
295
|
factory :post do
|
299
|
-
title "A title"
|
296
|
+
title { "A title" }
|
300
297
|
|
301
298
|
factory :approved_post do
|
302
|
-
approved true
|
299
|
+
approved { true }
|
303
300
|
end
|
304
301
|
end
|
305
302
|
|
@@ -312,11 +309,11 @@ You can also assign the parent explicitly:
|
|
312
309
|
|
313
310
|
```ruby
|
314
311
|
factory :post do
|
315
|
-
title "A title"
|
312
|
+
title { "A title" }
|
316
313
|
end
|
317
314
|
|
318
315
|
factory :approved_post, parent: :post do
|
319
|
-
approved true
|
316
|
+
approved { true }
|
320
317
|
end
|
321
318
|
```
|
322
319
|
|
@@ -392,20 +389,20 @@ FactoryBot.define do
|
|
392
389
|
|
393
390
|
# post factory with a `belongs_to` association for the user
|
394
391
|
factory :post do
|
395
|
-
title "Through the Looking Glass"
|
392
|
+
title { "Through the Looking Glass" }
|
396
393
|
user
|
397
394
|
end
|
398
395
|
|
399
396
|
# user factory without associated posts
|
400
397
|
factory :user do
|
401
|
-
name "John Doe"
|
398
|
+
name { "John Doe" }
|
402
399
|
|
403
400
|
# user_with_posts will create post data after the user has been created
|
404
401
|
factory :user_with_posts do
|
405
402
|
# posts_count is declared as a transient attribute and available in
|
406
403
|
# attributes on the factory, as well as the callback via the evaluator
|
407
404
|
transient do
|
408
|
-
posts_count 5
|
405
|
+
posts_count { 5 }
|
409
406
|
end
|
410
407
|
|
411
408
|
# the after(:create) yields two values; the user instance itself and the
|
@@ -441,13 +438,13 @@ FactoryBot.define do
|
|
441
438
|
|
442
439
|
# language factory with a `belongs_to` association for the profile
|
443
440
|
factory :language do
|
444
|
-
title "Through the Looking Glass"
|
441
|
+
title { "Through the Looking Glass" }
|
445
442
|
profile
|
446
443
|
end
|
447
444
|
|
448
445
|
# profile factory without associated languages
|
449
446
|
factory :profile do
|
450
|
-
name "John Doe"
|
447
|
+
name { "John Doe" }
|
451
448
|
|
452
449
|
# profile_with_languages will create language data after the profile has
|
453
450
|
# been created
|
@@ -455,7 +452,7 @@ FactoryBot.define do
|
|
455
452
|
# languages_count is declared as an ignored attribute and available in
|
456
453
|
# attributes on the factory, as well as the callback via the evaluator
|
457
454
|
transient do
|
458
|
-
languages_count 5
|
455
|
+
languages_count { 5 }
|
459
456
|
end
|
460
457
|
|
461
458
|
# the after(:create) yields two values; the profile instance itself and
|
@@ -598,25 +595,25 @@ to any factory.
|
|
598
595
|
factory :user, aliases: [:author]
|
599
596
|
|
600
597
|
factory :story do
|
601
|
-
title "My awesome story"
|
598
|
+
title { "My awesome story" }
|
602
599
|
author
|
603
600
|
|
604
601
|
trait :published do
|
605
|
-
published true
|
602
|
+
published { true }
|
606
603
|
end
|
607
604
|
|
608
605
|
trait :unpublished do
|
609
|
-
published false
|
606
|
+
published { false }
|
610
607
|
end
|
611
608
|
|
612
609
|
trait :week_long_publishing do
|
613
610
|
start_at { 1.week.ago }
|
614
|
-
end_at
|
611
|
+
end_at { Time.now }
|
615
612
|
end
|
616
613
|
|
617
614
|
trait :month_long_publishing do
|
618
615
|
start_at { 1.month.ago }
|
619
|
-
end_at
|
616
|
+
end_at { Time.now }
|
620
617
|
end
|
621
618
|
|
622
619
|
factory :week_long_published_story, traits: [:published, :week_long_publishing]
|
@@ -641,23 +638,23 @@ the trait that defines the attribute latest gets precedence.
|
|
641
638
|
|
642
639
|
```ruby
|
643
640
|
factory :user do
|
644
|
-
name "Friendly User"
|
641
|
+
name { "Friendly User" }
|
645
642
|
login { name }
|
646
643
|
|
647
644
|
trait :male do
|
648
|
-
name
|
649
|
-
gender "Male"
|
645
|
+
name { "John Doe" }
|
646
|
+
gender { "Male" }
|
650
647
|
login { "#{name} (M)" }
|
651
648
|
end
|
652
649
|
|
653
650
|
trait :female do
|
654
|
-
name
|
655
|
-
gender "Female"
|
651
|
+
name { "Jane Doe" }
|
652
|
+
gender { "Female" }
|
656
653
|
login { "#{name} (F)" }
|
657
654
|
end
|
658
655
|
|
659
656
|
trait :admin do
|
660
|
-
admin true
|
657
|
+
admin { true }
|
661
658
|
login { "admin-#{name}" }
|
662
659
|
end
|
663
660
|
|
@@ -670,18 +667,18 @@ You can also override individual attributes granted by a trait in subclasses.
|
|
670
667
|
|
671
668
|
```ruby
|
672
669
|
factory :user do
|
673
|
-
name "Friendly User"
|
670
|
+
name { "Friendly User" }
|
674
671
|
login { name }
|
675
672
|
|
676
673
|
trait :male do
|
677
|
-
name
|
678
|
-
gender "Male"
|
674
|
+
name { "John Doe" }
|
675
|
+
gender { "Male" }
|
679
676
|
login { "#{name} (M)" }
|
680
677
|
end
|
681
678
|
|
682
679
|
factory :brandon do
|
683
680
|
male
|
684
|
-
name "Brandon"
|
681
|
+
name { "Brandon" }
|
685
682
|
end
|
686
683
|
end
|
687
684
|
```
|
@@ -690,15 +687,15 @@ Traits can also be passed in as a list of symbols when you construct an instance
|
|
690
687
|
|
691
688
|
```ruby
|
692
689
|
factory :user do
|
693
|
-
name "Friendly User"
|
690
|
+
name { "Friendly User" }
|
694
691
|
|
695
692
|
trait :male do
|
696
|
-
name
|
697
|
-
gender "Male"
|
693
|
+
name { "John Doe" }
|
694
|
+
gender { "Male" }
|
698
695
|
end
|
699
696
|
|
700
697
|
trait :admin do
|
701
|
-
admin true
|
698
|
+
admin { true }
|
702
699
|
end
|
703
700
|
end
|
704
701
|
|
@@ -714,10 +711,10 @@ the number of instances to create/build as second parameter, as documented in th
|
|
714
711
|
|
715
712
|
```ruby
|
716
713
|
factory :user do
|
717
|
-
name "Friendly User"
|
714
|
+
name { "Friendly User" }
|
718
715
|
|
719
716
|
trait :admin do
|
720
|
-
admin true
|
717
|
+
admin { true }
|
721
718
|
end
|
722
719
|
end
|
723
720
|
|
@@ -729,10 +726,10 @@ Traits can be used with associations easily too:
|
|
729
726
|
|
730
727
|
```ruby
|
731
728
|
factory :user do
|
732
|
-
name "Friendly User"
|
729
|
+
name { "Friendly User" }
|
733
730
|
|
734
731
|
trait :admin do
|
735
|
-
admin true
|
732
|
+
admin { true }
|
736
733
|
end
|
737
734
|
end
|
738
735
|
|
@@ -748,10 +745,10 @@ When you're using association names that're different than the factory:
|
|
748
745
|
|
749
746
|
```ruby
|
750
747
|
factory :user do
|
751
|
-
name "Friendly User"
|
748
|
+
name { "Friendly User" }
|
752
749
|
|
753
750
|
trait :admin do
|
754
|
-
admin true
|
751
|
+
admin { true }
|
755
752
|
end
|
756
753
|
end
|
757
754
|
|
@@ -786,7 +783,7 @@ Finally, traits can accept transient attributes.
|
|
786
783
|
factory :invoice do
|
787
784
|
trait :with_amount do
|
788
785
|
transient do
|
789
|
-
amount 1
|
786
|
+
amount { 1 }
|
790
787
|
end
|
791
788
|
|
792
789
|
after(:create) do |invoice, evaluator|
|
@@ -860,7 +857,7 @@ FactoryBot.define do
|
|
860
857
|
after(:create) { |object| AuditLog.create(attrs: object.attributes) }
|
861
858
|
|
862
859
|
factory :user do
|
863
|
-
name "John Doe"
|
860
|
+
name { "John Doe" }
|
864
861
|
end
|
865
862
|
end
|
866
863
|
```
|
@@ -898,7 +895,7 @@ FactoryBot.define do
|
|
898
895
|
factory :user do
|
899
896
|
full_name "John Doe"
|
900
897
|
sequence(:username) { |n| "user#{n}" }
|
901
|
-
password "password"
|
898
|
+
password { "password" }
|
902
899
|
end
|
903
900
|
end
|
904
901
|
```
|
@@ -908,10 +905,10 @@ Instead of creating a child factory that added additional attributes:
|
|
908
905
|
```ruby
|
909
906
|
FactoryBot.define do
|
910
907
|
factory :application_user, parent: :user do
|
911
|
-
full_name
|
908
|
+
full_name { "Jane Doe" }
|
912
909
|
date_of_birth { 21.years.ago }
|
913
|
-
gender
|
914
|
-
health
|
910
|
+
gender { "Female" }
|
911
|
+
health { 90 }
|
915
912
|
end
|
916
913
|
end
|
917
914
|
```
|
@@ -921,10 +918,10 @@ You could modify that factory instead.
|
|
921
918
|
```ruby
|
922
919
|
FactoryBot.modify do
|
923
920
|
factory :user do
|
924
|
-
full_name
|
921
|
+
full_name { "Jane Doe" }
|
925
922
|
date_of_birth { 21.years.ago }
|
926
|
-
gender
|
927
|
-
health
|
923
|
+
gender { "Female" }
|
924
|
+
health { 90 }
|
928
925
|
end
|
929
926
|
end
|
930
927
|
```
|
@@ -1072,7 +1069,7 @@ end
|
|
1072
1069
|
sequence(:email) { |n| "person#{n}@example.com" }
|
1073
1070
|
|
1074
1071
|
factory :user do
|
1075
|
-
name "Jane Doe"
|
1072
|
+
name { "Jane Doe" }
|
1076
1073
|
email
|
1077
1074
|
|
1078
1075
|
initialize_with { new(name) }
|
@@ -1102,7 +1099,7 @@ For example:
|
|
1102
1099
|
|
1103
1100
|
```ruby
|
1104
1101
|
factory :user do
|
1105
|
-
name "John Doe"
|
1102
|
+
name { "John Doe" }
|
1106
1103
|
|
1107
1104
|
initialize_with { User.build_with_name(name) }
|
1108
1105
|
end
|
@@ -1114,7 +1111,7 @@ by calling `attributes`:
|
|
1114
1111
|
```ruby
|
1115
1112
|
factory :user do
|
1116
1113
|
transient do
|
1117
|
-
comments_count 5
|
1114
|
+
comments_count { 5 }
|
1118
1115
|
end
|
1119
1116
|
|
1120
1117
|
name "John Doe"
|
@@ -1125,7 +1122,7 @@ end
|
|
1125
1122
|
|
1126
1123
|
This will build a hash of all attributes to be passed to `new`. It won't
|
1127
1124
|
include transient attributes, but everything else defined in the factory will be
|
1128
|
-
passed (associations,
|
1125
|
+
passed (associations, evaluated sequences, etc.)
|
1129
1126
|
|
1130
1127
|
You can define `initialize_with` for all factories by including it in the
|
1131
1128
|
`FactoryBot.define` block:
|
@@ -1287,7 +1284,7 @@ FactoryBot.define do
|
|
1287
1284
|
|
1288
1285
|
|
1289
1286
|
factory :user do
|
1290
|
-
name "John Doe"
|
1287
|
+
name { "John Doe" }
|
1291
1288
|
end
|
1292
1289
|
end
|
1293
1290
|
```
|
@@ -1341,12 +1338,12 @@ with associations, as below:
|
|
1341
1338
|
```ruby
|
1342
1339
|
FactoryBot.define do
|
1343
1340
|
factory :united_states, class: Location do
|
1344
|
-
name 'United States'
|
1341
|
+
name { 'United States' }
|
1345
1342
|
association :location_group, factory: :north_america
|
1346
1343
|
end
|
1347
1344
|
|
1348
1345
|
factory :north_america, class: LocationGroup do
|
1349
|
-
name 'North America'
|
1346
|
+
name { 'North America' }
|
1350
1347
|
end
|
1351
1348
|
end
|
1352
1349
|
```
|
@@ -1378,7 +1375,7 @@ require 'factory_bot'
|
|
1378
1375
|
```
|
1379
1376
|
|
1380
1377
|
Once required, assuming you have a directory structure of `spec/factories` or
|
1381
|
-
`test/factories`, all you'll need to do is run
|
1378
|
+
`test/factories`, all you'll need to do is run:
|
1382
1379
|
|
1383
1380
|
```ruby
|
1384
1381
|
FactoryBot.find_definitions
|
@@ -1400,7 +1397,7 @@ require 'factory_bot'
|
|
1400
1397
|
|
1401
1398
|
FactoryBot.define do
|
1402
1399
|
factory :user do
|
1403
|
-
name 'John Doe'
|
1400
|
+
name { 'John Doe' }
|
1404
1401
|
date_of_birth { 21.years.ago }
|
1405
1402
|
end
|
1406
1403
|
end
|