factory_bot 6.5.1 → 6.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 71c90d06521a4d6e1bbd613bdc1494919b2acde4117153524a11d8fbea53301c
4
- data.tar.gz: 7e19bf127cf040c9f59fea525894019358e0502de695cf5957ac5eb91280e1d1
3
+ metadata.gz: 5268f67c49f294395edabe86ef2bf7bdd3644db872328f0134d832b8cbfe5396
4
+ data.tar.gz: 5e51702b4c819af99587ea2355b43b5770b228e1f0b81483b1bc9aa28806c630
5
5
  SHA512:
6
- metadata.gz: ff1b56b976b423b91d8f633fdb0d1ed2d13b5c75665547adbb4d6f1d90b15aff35459dcb2fd6479e6dccdfd98ce3a9bd2a13b1ced002704fb84c39e2d5ebb3f3
7
- data.tar.gz: dfc7713d8511e0723ab41da77257c901ca2d5abdf04b21b8f81332bd4f6e6568d403a68fc7831529ab8cbed531c25bc068458ee28fb2b1d7f0b7ebeafda02a3a
6
+ metadata.gz: 80f08c8d63755aa32390aadfd2b5c653915a2d8920b58d56b89a6091c029a0194a007b5f6c99277f94c175a0c0c83463ce24d5849268ac8d3ab76c059379116d
7
+ data.tar.gz: f092552d291c38644d02d77a73722b5966cc33397a1dcd06cb90164da65779c14f8a0352c77fd19890d30ccbe112d66a21c130865486da0b8059168e3f4a30e7
data/GETTING_STARTED.md CHANGED
@@ -17,9 +17,9 @@ at any time.
17
17
  Getting Started
18
18
  ===============
19
19
 
20
- * [Setup](#setup)
21
- + [Update Your Gemfile](#update-your-gemfile)
22
- + [Configure your test suite](#configure-your-test-suite)
20
+ - [Setup](#setup)
21
+ - [Update Your Gemfile](#update-your-gemfile)
22
+ - [Configure your test suite](#configure-your-test-suite)
23
23
  - [RSpec](#rspec)
24
24
  - [Test::Unit](#testunit)
25
25
  - [Cucumber](#cucumber)
@@ -27,76 +27,77 @@ Getting Started
27
27
  - [Minitest](#minitest)
28
28
  - [Minitest::Spec](#minitestspec)
29
29
  - [minitest-rails](#minitest-rails)
30
- * [Defining factories](#defining-factories)
31
- + [Factory name and attributes](#factory-name-and-attributes)
32
- + [Specifying the class explicitly](#specifying-the-class-explicitly)
33
- + [Hash attributes](#hash-attributes)
34
- + [Best practices](#best-practices)
35
- + [Definition file paths](#definition-file-paths)
36
- + [Static Attributes](#static-attributes)
37
- * [Using factories](#using-factories)
38
- + [Build strategies](#build-strategies)
39
- + [Attribute overrides](#attribute-overrides)
40
- + [`build_stubbed` and `Marshal.dump`](#build_stubbed-and-marshaldump)
41
- * [Aliases](#aliases)
42
- * [Dependent Attributes](#dependent-attributes)
43
- * [Transient Attributes](#transient-attributes)
44
- + [With other attributes](#with-other-attributes)
45
- + [With `attributes_for`](#with-attributes_for)
46
- + [With callbacks](#with-callbacks)
47
- + [With associations](#with-associations)
48
- * [Method Name / Reserved Word Attributes](#method-name--reserved-word-attributes)
49
- * [Inheritance](#inheritance)
50
- + [Nested factories](#nested-factories)
51
- + [Assigning parent explicitly](#assigning-parent-explicitly)
52
- + [Best practices](#best-practices-1)
53
- * [Associations](#associations)
54
- + [Implicit definition](#implicit-definition)
55
- + [Explicit definition](#explicit-definition)
56
- + [Inline definition](#inline-definition)
57
- + [Specifying the factory](#specifying-the-factory)
58
- + [Overriding attributes](#overriding-attributes)
59
- + [Association overrides](#association-overrides)
60
- + [Build strategies](#build-strategies-1)
61
- + [`has_many` associations](#has_many-associations)
62
- + [`has_and_belongs_to_many` associations](#has_and_belongs_to_many-associations)
63
- + [Polymorphic associations](#polymorphic-associations)
64
- + [Interconnected associations](#interconnected-associations)
65
- * [Sequences](#sequences)
66
- + [Global sequences](#global-sequences)
67
- + [With dynamic attributes](#with-dynamic-attributes)
68
- + [As implicit attributes](#as-implicit-attributes)
69
- + [Inline sequences](#inline-sequences)
70
- + [Initial value](#initial-value)
71
- + [Without a block](#without-a-block)
72
- + [Aliases](#aliases-1)
73
- + [Rewinding](#rewinding)
74
- + [Uniqueness](#uniqueness)
75
- * [Traits](#traits)
76
- + [Defining traits](#defining-traits)
77
- + [As implicit attributes](#as-implicit-attributes-1)
78
- + [Attribute precedence](#attribute-precedence)
79
- + [In child factories](#in-child-factories)
80
- + [Using traits](#using-traits)
81
- + [With associations](#with-associations-1)
82
- + [Traits within traits](#traits-within-traits)
83
- + [With transient attributes](#with-transient-attributes)
84
- + [Enum traits](#enum-traits)
85
- * [Callbacks](#callbacks)
86
- + [Default callbacks](#default-callbacks)
87
- + [Multiple callbacks](#multiple-callbacks)
88
- + [Global callbacks](#global-callbacks)
89
- + [Symbol#to_proc](#symbolto_proc)
90
- * [Modifying factories](#modifying-factories)
91
- * [Building or Creating Multiple Records](#building-or-creating-multiple-records)
92
- * [Linting Factories](#linting-factories)
93
- * [Custom Construction](#custom-construction)
94
- * [Custom Strategies](#custom-strategies)
95
- * [Custom Callbacks](#custom-callbacks)
96
- * [Custom Methods to Persist Objects](#custom-methods-to-persist-objects)
97
- * [ActiveSupport Instrumentation](#activesupport-instrumentation)
98
- * [Rails Preloaders and RSpec](#rails-preloaders-and-rspec)
99
- * [Using Without Bundler](#using-without-bundler)
30
+ - [Defining factories](#defining-factories)
31
+ - [Factory name and attributes](#factory-name-and-attributes)
32
+ - [Specifying the class explicitly](#specifying-the-class-explicitly)
33
+ - [Hash attributes](#hash-attributes)
34
+ - [Best practices](#best-practices)
35
+ - [Definition file paths](#definition-file-paths)
36
+ - [Static Attributes](#static-attributes)
37
+ - [Using factories](#using-factories)
38
+ - [Build strategies](#build-strategies)
39
+ - [Attribute overrides](#attribute-overrides)
40
+ - [build\_stubbed and Marshal.dump](#build_stubbed-and-marshaldump)
41
+ - [Aliases](#aliases)
42
+ - [Dependent Attributes](#dependent-attributes)
43
+ - [Transient Attributes](#transient-attributes)
44
+ - [With other attributes](#with-other-attributes)
45
+ - [With attributes\_for](#with-attributes_for)
46
+ - [With callbacks](#with-callbacks)
47
+ - [With associations](#with-associations)
48
+ - [Method Name / Reserved Word Attributes](#method-name--reserved-word-attributes)
49
+ - [Inheritance](#inheritance)
50
+ - [Nested factories](#nested-factories)
51
+ - [Assigning parent explicitly](#assigning-parent-explicitly)
52
+ - [Best practices](#best-practices-1)
53
+ - [Associations](#associations)
54
+ - [Implicit definition](#implicit-definition)
55
+ - [Explicit definition](#explicit-definition)
56
+ - [Inline definition](#inline-definition)
57
+ - [Specifying the factory](#specifying-the-factory)
58
+ - [Overriding attributes](#overriding-attributes)
59
+ - [Association overrides](#association-overrides)
60
+ - [Build strategies](#build-strategies-1)
61
+ - [has\_many associations](#has_many-associations)
62
+ - [has\_and\_belongs\_to\_many associations](#has_and_belongs_to_many-associations)
63
+ - [Polymorphic associations](#polymorphic-associations)
64
+ - [Interconnected associations](#interconnected-associations)
65
+ - [Sequences](#sequences)
66
+ - [Global sequences](#global-sequences)
67
+ - [With dynamic attributes](#with-dynamic-attributes)
68
+ - [As implicit attributes](#as-implicit-attributes)
69
+ - [Inline sequences](#inline-sequences)
70
+ - [Initial value](#initial-value)
71
+ - [Without a block](#without-a-block)
72
+ - [Aliases](#aliases-1)
73
+ - [Rewinding](#rewinding)
74
+ - [Uniqueness](#uniqueness)
75
+ - [Traits](#traits)
76
+ - [Defining traits](#defining-traits)
77
+ - [As implicit attributes](#as-implicit-attributes-1)
78
+ - [Attribute precedence](#attribute-precedence)
79
+ - [In child factories](#in-child-factories)
80
+ - [As mixins](#as-mixins)
81
+ - [Using traits](#using-traits)
82
+ - [With associations](#with-associations-1)
83
+ - [Traits within traits](#traits-within-traits)
84
+ - [With transient attributes](#with-transient-attributes)
85
+ - [Enum traits](#enum-traits)
86
+ - [Callbacks](#callbacks)
87
+ - [Default callbacks](#default-callbacks)
88
+ - [Multiple callbacks](#multiple-callbacks)
89
+ - [Global callbacks](#global-callbacks)
90
+ - [Symbol#to\_proc](#symbolto_proc)
91
+ - [Modifying factories](#modifying-factories)
92
+ - [Building or Creating Multiple Records](#building-or-creating-multiple-records)
93
+ - [Linting Factories](#linting-factories)
94
+ - [Custom Construction](#custom-construction)
95
+ - [Custom Strategies](#custom-strategies)
96
+ - [Custom Callbacks](#custom-callbacks)
97
+ - [Custom Methods to Persist Objects](#custom-methods-to-persist-objects)
98
+ - [ActiveSupport Instrumentation](#activesupport-instrumentation)
99
+ - [Rails Preloaders and RSpec](#rails-preloaders-and-rspec)
100
+ - [Using Without Bundler](#using-without-bundler)
100
101
 
101
102
  Setup
102
103
  -----
@@ -219,6 +220,26 @@ It is also possible to explicitly specify the class:
219
220
  factory :admin, class: "User"
220
221
  ```
221
222
 
223
+ Explicit specification of the class, _with the full namespace_, is necessary when defining factories for classes nested within other modules or classes:
224
+
225
+ ```ruby
226
+ # foo/bar.rb
227
+ module Foo
228
+ class Bar
229
+ ...
230
+ end
231
+ end
232
+
233
+ # factories.rb
234
+ FactoryBot.define do
235
+ factory :bar, class: 'Foo::Bar' do
236
+ ...
237
+ end
238
+ end
239
+ ```
240
+
241
+ If the full namespace is not provided in the `factory` statement, you will receive a `NameError: uninitialized constant Bar` error.
242
+
222
243
  You can pass a constant as well, if the constant is available (note that this
223
244
  can cause test performance problems in large Rails applications, since
224
245
  referring to the constant will cause it to be eagerly loaded).
@@ -255,10 +276,12 @@ Factories can be defined anywhere, but will be automatically loaded after
255
276
  calling `FactoryBot.find_definitions` if factories are defined in files at the
256
277
  following locations:
257
278
 
279
+ factories.rb
280
+ factories/**/*.rb
258
281
  test/factories.rb
282
+ test/factories/**/*.rb
259
283
  spec/factories.rb
260
- test/factories/*.rb
261
- spec/factories/*.rb
284
+ spec/factories/**/*.rb
262
285
 
263
286
  ### Static Attributes
264
287
 
@@ -332,7 +355,7 @@ user = build(:user, account:, friends:)
332
355
 
333
356
  [omitting values]: https://docs.ruby-lang.org/en/3.1/syntax/literals_rdoc.html#label-Hash+Literals
334
357
 
335
- ### `build_stubbed` and `Marshal.dump`
358
+ ### build_stubbed and Marshal.dump
336
359
 
337
360
  Note that objects created with `build_stubbed` cannot be serialized with
338
361
  `Marshal.dump`, since factory\_bot defines singleton methods on these objects.
@@ -413,7 +436,7 @@ create(:user, rockstar: false).name
413
436
  #=> "John Doe"
414
437
  ```
415
438
 
416
- ### With `attributes_for`
439
+ ### With attributes_for
417
440
 
418
441
  Transient attributes will be ignored within attributes\_for and won't be set on
419
442
  the model, even if the attribute exists or you attempt to override it.
@@ -619,7 +642,7 @@ end
619
642
 
620
643
  Or inline using attributes from the factory:
621
644
 
622
- ```rb
645
+ ```ruby
623
646
  factory :post do
624
647
  # ...
625
648
  author_last_name { "Writely" }
@@ -713,7 +736,7 @@ factory :post do
713
736
  author strategy: :build # <<< this does *not* work; causes author_id to be nil
714
737
  ```
715
738
 
716
- ### `has_many` associations
739
+ ### has_many associations
717
740
 
718
741
  There are a few ways to generate data for a `has_many` relationship. The
719
742
  simplest approach is to write a helper method in plain Ruby to tie together the
@@ -840,7 +863,7 @@ build(:user_with_posts, posts_count: 15).posts.length # 15
840
863
  build_stubbed(:user_with_posts, posts_count: 15).posts.length # 15
841
864
  ```
842
865
 
843
- ### `has_and_belongs_to_many` associations
866
+ ### has_and_belongs_to_many associations
844
867
 
845
868
  Generating data for a `has_and_belongs_to_many` relationship is very similar
846
869
  to the above `has_many` relationship, with a small change: you need to pass an
@@ -1130,7 +1153,7 @@ When working with uniqueness constraints, be careful not to pass in override val
1130
1153
 
1131
1154
  In this example the email will be the same for both users. If email must be unique, this code will error:
1132
1155
 
1133
- ```rb
1156
+ ```ruby
1134
1157
  factory :user do
1135
1158
  sequence(:email) { |n| "person#{n}@example.com" }
1136
1159
  end
@@ -1396,7 +1419,7 @@ create :invoice, :with_amount, amount: 2
1396
1419
 
1397
1420
  Given an Active Record model with an enum attribute:
1398
1421
 
1399
- ```rb
1422
+ ```ruby
1400
1423
  class Task < ActiveRecord::Base
1401
1424
  enum status: {queued: 0, started: 1, finished: 2}
1402
1425
  end
@@ -1406,7 +1429,7 @@ end
1406
1429
  factory\_bot will automatically define traits for each possible value of the
1407
1430
  enum:
1408
1431
 
1409
- ```rb
1432
+ ```ruby
1410
1433
  FactoryBot.define do
1411
1434
  factory :task
1412
1435
  end
@@ -1418,7 +1441,7 @@ FactoryBot.build(:task, :finished)
1418
1441
 
1419
1442
  Writing the traits out manually would be cumbersome, and is not necessary:
1420
1443
 
1421
- ```rb
1444
+ ```ruby
1422
1445
  FactoryBot.define do
1423
1446
  factory :task do
1424
1447
  trait :queued do
@@ -1443,7 +1466,7 @@ desired, it is possible to disable the feature by setting
1443
1466
  In that case, it is still possible to explicitly define traits for an enum
1444
1467
  attribute in a particular factory:
1445
1468
 
1446
- ```rb
1469
+ ```ruby
1447
1470
  FactoryBot.automatically_define_enum_traits = false
1448
1471
 
1449
1472
  FactoryBot.define do
@@ -1458,7 +1481,7 @@ specifically tied to Active Record enum attributes.
1458
1481
 
1459
1482
  With an array:
1460
1483
 
1461
- ```rb
1484
+ ```ruby
1462
1485
  class Task
1463
1486
  attr_accessor :status
1464
1487
  end
@@ -1472,7 +1495,7 @@ end
1472
1495
 
1473
1496
  Or with a hash:
1474
1497
 
1475
- ```rb
1498
+ ```ruby
1476
1499
  class Task
1477
1500
  attr_accessor :status
1478
1501
  end