factory_bot 6.5.4 → 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 +4 -4
- data/GETTING_STARTED.md +106 -85
- data/NEWS.md +430 -334
- data/README.md +11 -19
- data/lib/factory_bot/attribute_assigner.rb +74 -18
- data/lib/factory_bot/callbacks_observer.rb +19 -1
- data/lib/factory_bot/evaluator.rb +9 -3
- data/lib/factory_bot/factory.rb +8 -4
- data/lib/factory_bot/registry.rb +1 -1
- data/lib/factory_bot/sequence.rb +24 -10
- data/lib/factory_bot/strategy/build.rb +2 -0
- data/lib/factory_bot/strategy/create.rb +2 -0
- data/lib/factory_bot/strategy.rb +15 -0
- data/lib/factory_bot/syntax/methods.rb +7 -9
- data/lib/factory_bot/version.rb +1 -1
- data/lib/factory_bot.rb +1 -6
- metadata +5 -5
- data/lib/factory_bot/strategy_calculator.rb +0 -26
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 5268f67c49f294395edabe86ef2bf7bdd3644db872328f0134d832b8cbfe5396
         | 
| 4 | 
            +
              data.tar.gz: 5e51702b4c819af99587ea2355b43b5770b228e1f0b81483b1bc9aa28806c630
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 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 | 
            -
             | 
| 21 | 
            -
               | 
| 22 | 
            -
               | 
| 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 | 
            -
             | 
| 31 | 
            -
               | 
| 32 | 
            -
               | 
| 33 | 
            -
               | 
| 34 | 
            -
               | 
| 35 | 
            -
               | 
| 36 | 
            -
               | 
| 37 | 
            -
             | 
| 38 | 
            -
               | 
| 39 | 
            -
               | 
| 40 | 
            -
               | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
               | 
| 45 | 
            -
               | 
| 46 | 
            -
               | 
| 47 | 
            -
               | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
               | 
| 51 | 
            -
               | 
| 52 | 
            -
               | 
| 53 | 
            -
             | 
| 54 | 
            -
               | 
| 55 | 
            -
               | 
| 56 | 
            -
               | 
| 57 | 
            -
               | 
| 58 | 
            -
               | 
| 59 | 
            -
               | 
| 60 | 
            -
               | 
| 61 | 
            -
               | 
| 62 | 
            -
               | 
| 63 | 
            -
               | 
| 64 | 
            -
               | 
| 65 | 
            -
             | 
| 66 | 
            -
               | 
| 67 | 
            -
               | 
| 68 | 
            -
               | 
| 69 | 
            -
               | 
| 70 | 
            -
               | 
| 71 | 
            -
               | 
| 72 | 
            -
               | 
| 73 | 
            -
               | 
| 74 | 
            -
               | 
| 75 | 
            -
             | 
| 76 | 
            -
               | 
| 77 | 
            -
               | 
| 78 | 
            -
               | 
| 79 | 
            -
               | 
| 80 | 
            -
               | 
| 81 | 
            -
               | 
| 82 | 
            -
               | 
| 83 | 
            -
               | 
| 84 | 
            -
               | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
               | 
| 88 | 
            -
               | 
| 89 | 
            -
               | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 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).
         | 
| @@ -334,7 +355,7 @@ user = build(:user, account:, friends:) | |
| 334 355 |  | 
| 335 356 | 
             
            [omitting values]: https://docs.ruby-lang.org/en/3.1/syntax/literals_rdoc.html#label-Hash+Literals
         | 
| 336 357 |  | 
| 337 | 
            -
            ###  | 
| 358 | 
            +
            ### build_stubbed and Marshal.dump
         | 
| 338 359 |  | 
| 339 360 | 
             
            Note that objects created with `build_stubbed` cannot be serialized with
         | 
| 340 361 | 
             
            `Marshal.dump`, since factory\_bot defines singleton methods on these objects.
         | 
| @@ -415,7 +436,7 @@ create(:user, rockstar: false).name | |
| 415 436 | 
             
            #=> "John Doe"
         | 
| 416 437 | 
             
            ```
         | 
| 417 438 |  | 
| 418 | 
            -
            ### With  | 
| 439 | 
            +
            ### With attributes_for
         | 
| 419 440 |  | 
| 420 441 | 
             
            Transient attributes will be ignored within attributes\_for and won't be set on
         | 
| 421 442 | 
             
            the model, even if the attribute exists or you attempt to override it.
         | 
| @@ -621,7 +642,7 @@ end | |
| 621 642 |  | 
| 622 643 | 
             
            Or inline using attributes from the factory:
         | 
| 623 644 |  | 
| 624 | 
            -
            ``` | 
| 645 | 
            +
            ```ruby
         | 
| 625 646 | 
             
            factory :post do
         | 
| 626 647 | 
             
              # ...
         | 
| 627 648 | 
             
              author_last_name { "Writely" }
         | 
| @@ -715,7 +736,7 @@ factory :post do | |
| 715 736 | 
             
              author strategy: :build    # <<< this does *not* work; causes author_id to be nil
         | 
| 716 737 | 
             
            ```
         | 
| 717 738 |  | 
| 718 | 
            -
            ###  | 
| 739 | 
            +
            ### has_many associations
         | 
| 719 740 |  | 
| 720 741 | 
             
            There are a few ways to generate data for a `has_many` relationship. The
         | 
| 721 742 | 
             
            simplest approach is to write a helper method in plain Ruby to tie together the
         | 
| @@ -842,7 +863,7 @@ build(:user_with_posts, posts_count: 15).posts.length # 15 | |
| 842 863 | 
             
            build_stubbed(:user_with_posts, posts_count: 15).posts.length # 15
         | 
| 843 864 | 
             
            ```
         | 
| 844 865 |  | 
| 845 | 
            -
            ###  | 
| 866 | 
            +
            ### has_and_belongs_to_many associations
         | 
| 846 867 |  | 
| 847 868 | 
             
            Generating data for a `has_and_belongs_to_many` relationship is very similar
         | 
| 848 869 | 
             
            to the above `has_many` relationship, with a small change: you need to pass an
         | 
| @@ -1132,7 +1153,7 @@ When working with uniqueness constraints, be careful not to pass in override val | |
| 1132 1153 |  | 
| 1133 1154 | 
             
            In this example the email will be the same for both users. If email must be unique, this code will error:
         | 
| 1134 1155 |  | 
| 1135 | 
            -
            ``` | 
| 1156 | 
            +
            ```ruby
         | 
| 1136 1157 | 
             
            factory :user do
         | 
| 1137 1158 | 
             
              sequence(:email) { |n| "person#{n}@example.com" }
         | 
| 1138 1159 | 
             
            end
         | 
| @@ -1398,7 +1419,7 @@ create :invoice, :with_amount, amount: 2 | |
| 1398 1419 |  | 
| 1399 1420 | 
             
            Given an Active Record model with an enum attribute:
         | 
| 1400 1421 |  | 
| 1401 | 
            -
            ``` | 
| 1422 | 
            +
            ```ruby
         | 
| 1402 1423 | 
             
            class Task < ActiveRecord::Base
         | 
| 1403 1424 | 
             
              enum status: {queued: 0, started: 1, finished: 2}
         | 
| 1404 1425 | 
             
            end
         | 
| @@ -1408,7 +1429,7 @@ end | |
| 1408 1429 | 
             
            factory\_bot will automatically define traits for each possible value of the
         | 
| 1409 1430 | 
             
            enum:
         | 
| 1410 1431 |  | 
| 1411 | 
            -
            ``` | 
| 1432 | 
            +
            ```ruby
         | 
| 1412 1433 | 
             
            FactoryBot.define do
         | 
| 1413 1434 | 
             
              factory :task
         | 
| 1414 1435 | 
             
            end
         | 
| @@ -1420,7 +1441,7 @@ FactoryBot.build(:task, :finished) | |
| 1420 1441 |  | 
| 1421 1442 | 
             
            Writing the traits out manually would be cumbersome, and is not necessary:
         | 
| 1422 1443 |  | 
| 1423 | 
            -
            ``` | 
| 1444 | 
            +
            ```ruby
         | 
| 1424 1445 | 
             
            FactoryBot.define do
         | 
| 1425 1446 | 
             
              factory :task do
         | 
| 1426 1447 | 
             
                trait :queued do
         | 
| @@ -1445,7 +1466,7 @@ desired, it is possible to disable the feature by setting | |
| 1445 1466 | 
             
            In that case, it is still possible to explicitly define traits for an enum
         | 
| 1446 1467 | 
             
            attribute in a particular factory:
         | 
| 1447 1468 |  | 
| 1448 | 
            -
            ``` | 
| 1469 | 
            +
            ```ruby
         | 
| 1449 1470 | 
             
            FactoryBot.automatically_define_enum_traits = false
         | 
| 1450 1471 |  | 
| 1451 1472 | 
             
            FactoryBot.define do
         | 
| @@ -1460,7 +1481,7 @@ specifically tied to Active Record enum attributes. | |
| 1460 1481 |  | 
| 1461 1482 | 
             
            With an array:
         | 
| 1462 1483 |  | 
| 1463 | 
            -
            ``` | 
| 1484 | 
            +
            ```ruby
         | 
| 1464 1485 | 
             
            class Task
         | 
| 1465 1486 | 
             
              attr_accessor :status
         | 
| 1466 1487 | 
             
            end
         | 
| @@ -1474,7 +1495,7 @@ end | |
| 1474 1495 |  | 
| 1475 1496 | 
             
            Or with a hash:
         | 
| 1476 1497 |  | 
| 1477 | 
            -
            ``` | 
| 1498 | 
            +
            ```ruby
         | 
| 1478 1499 | 
             
            class Task
         | 
| 1479 1500 | 
             
              attr_accessor :status
         | 
| 1480 1501 | 
             
            end
         |