diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 9747e1f..7481d55 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -347,9 +347,35 @@ factory :post do end ``` -The behavior of the association method varies depending on the build strategy used for the parent object. +In factory\_bot 5, associations default to using the same build strategy as +their parent object: ```ruby +FactoryBot.define do + factory :author + + factory :post do + author + end +end + +post = build(:post) +post.new_record? # => true +post.author.new_record? # => true + +post = create(:post) +post.new_record? # => false +post.author.new_record? # => false +``` + +This is different than the default behavior for previous versions of +factory\_bot, where the association strategy would not always match the strategy +of the parent object. If you want to continue using the old behavior, you can +set the `use_parent_strategy` configuration option to `false`. + +```ruby +FactoryBot.use_parent_strategy = false + # Builds and saves a User and a Post post = create(:post) post.new_record? # => false @@ -364,6 +390,8 @@ post.author.new_record? # => false To not save the associated object, specify strategy: :build in the factory: ```ruby +FactoryBot.use_parent_strategy = false + factory :post do # ... association :author, factory: :user, strategy: :build @@ -384,28 +412,6 @@ factory :post do author strategy: :build # <<< this does *not* work; causes author_id to be nil ``` -To have unspecified associations use the parent's strategy, instead of using :create, you can use the configuration `use_parent_strategy`: - -```ruby -FactoryBot.use_parent_strategy = true - -factory :post do - # ... - author -end - -post = build(:post) -post.new_record? # => true -post.author.new_record? # => true - -post = create(:post) -post.new_record? # => false -post.author.new_record? # => false -``` - -If you are using rspec, you can set this configuration in your spec_helper.rb (or rails_helper.rb, if using Rails). -If you want to set it globally, you can use an intializer (if using Rails). - Generating data for a `has_many` relationship is a bit more involved, depending on the amount of flexibility desired, but here's a surefire example of generating associated data. diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index dceb0c7..62627b1 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -55,7 +55,7 @@ module FactoryBot @configuration = nil end - mattr_accessor :use_parent_strategy, instance_accessor: false, default: false + mattr_accessor :use_parent_strategy, instance_accessor: false, default: true # Look for errors in factories and (optionally) their traits. # Parameters: diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 755d593..030f4bf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,7 +20,7 @@ RSpec.configure do |config| config.before do FactoryBot.reload - FactoryBot.use_parent_strategy = false + FactoryBot.use_parent_strategy = true end config.after do