From 54f37563cde5e672419013d2d90367e68b75b17f Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Fri, 11 Jan 2019 06:39:39 +0000 Subject: [PATCH] Set use_parent_strategy default to true explicitly The `default:` option of `mattr_accessor` wasn't added until Rails 5.2, which means it has no effect on older versions and the default value of `FactoryBot.use_parent_strategy` in 5.0.0.rc1 is nil on those versions. --- lib/factory_bot.rb | 3 ++- spec/factory_bot_spec.rb | 6 ++++++ spec/spec_helper.rb | 10 +++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index b179af4..feebbf1 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -57,7 +57,8 @@ module FactoryBot @configuration = nil end - mattr_accessor :use_parent_strategy, instance_accessor: false, default: true + mattr_accessor :use_parent_strategy, instance_accessor: false + self.use_parent_strategy = true # Look for errors in factories and (optionally) their traits. # Parameters: diff --git a/spec/factory_bot_spec.rb b/spec/factory_bot_spec.rb index f282923..06e654c 100644 --- a/spec/factory_bot_spec.rb +++ b/spec/factory_bot_spec.rb @@ -17,4 +17,10 @@ describe FactoryBot do FactoryBot.register_trait(trait) expect(FactoryBot.trait_by_name(trait.name)).to eq trait end + + describe ".use_parent_strategy" do + it "is true by default" do + expect(FactoryBot.use_parent_strategy).to be true + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 030f4bf..269f26d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,13 +20,21 @@ RSpec.configure do |config| config.before do FactoryBot.reload - FactoryBot.use_parent_strategy = true end config.after do Timecop.return end + config.around do |example| + begin + previous_use_parent_strategy = FactoryBot.use_parent_strategy + example.run + ensure + FactoryBot.use_parent_strategy = previous_use_parent_strategy + end + end + config.order = :random Kernel.srand config.seed