diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index 2330397..dceb0c7 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -1,5 +1,6 @@ require "set" require "active_support/core_ext/module/delegation" +require "active_support/core_ext/module/attribute_accessors" require "active_support/deprecation" require "active_support/notifications" @@ -54,6 +55,8 @@ module FactoryBot @configuration = nil end + mattr_accessor :use_parent_strategy, instance_accessor: false, default: false + # Look for errors in factories and (optionally) their traits. # Parameters: # factories - which factories to lint; omit for all factories @@ -78,8 +81,6 @@ module FactoryBot :skip_create, :initialize_with, :constructor, - :use_parent_strategy, - :use_parent_strategy=, to: :configuration end diff --git a/lib/factory_bot/configuration.rb b/lib/factory_bot/configuration.rb index 67b5817..86afefd 100644 --- a/lib/factory_bot/configuration.rb +++ b/lib/factory_bot/configuration.rb @@ -3,8 +3,6 @@ module FactoryBot class Configuration attr_reader :factories, :sequences, :traits, :strategies, :callback_names - attr_accessor :use_parent_strategy - def initialize @factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Factory")) @sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Sequence")) diff --git a/spec/acceptance/build_spec.rb b/spec/acceptance/build_spec.rb index 2bff7d9..955d6f6 100644 --- a/spec/acceptance/build_spec.rb +++ b/spec/acceptance/build_spec.rb @@ -21,8 +21,8 @@ describe "a built instance" do it { should be_new_record } - context "when the :use_parent_strategy config option has not been set" do - before { FactoryBot.use_parent_strategy = nil } + context "when the :use_parent_strategy config option is set to false" do + before { FactoryBot.use_parent_strategy = false } it "assigns and saves associations" do expect(subject.user).to be_kind_of(User) @@ -30,7 +30,7 @@ describe "a built instance" do end end - context "when the :use_parent_strategy config option has been enabled" do + context "when the :use_parent_strategy config option is set to true" do before { FactoryBot.use_parent_strategy = true } it "assigns but does not save associations" do @@ -38,13 +38,6 @@ describe "a built instance" do expect(subject.user).to be_new_record end end - - it "assigns but does not save associations when using parent strategy" do - FactoryBot.use_parent_strategy = true - - expect(subject.user).to be_kind_of(User) - expect(subject.user).to be_new_record - end end describe "a built instance with strategy: :create" do diff --git a/spec/acceptance/register_strategies_spec.rb b/spec/acceptance/register_strategies_spec.rb index 9bafb95..19aea5e 100644 --- a/spec/acceptance/register_strategies_spec.rb +++ b/spec/acceptance/register_strategies_spec.rb @@ -98,8 +98,8 @@ describe "associations without overriding :strategy" do end end - context "when the :use_parent_strategy config option has not been enabled" do - before { FactoryBot.use_parent_strategy = nil } + context "when the :use_parent_strategy config option is set to false" do + before { FactoryBot.use_parent_strategy = false } it "uses the overridden strategy on the association" do FactoryBot.register_strategy(:create, custom_strategy) @@ -109,7 +109,7 @@ describe "associations without overriding :strategy" do end end - context "when the :use_parent_strategy config option has been enabled" do + context "when the :use_parent_strategy config option is set to true" do before { FactoryBot.use_parent_strategy = true } it "uses the parent strategy on the association" do diff --git a/spec/acceptance/reload_spec.rb b/spec/acceptance/reload_spec.rb new file mode 100644 index 0000000..28017b9 --- /dev/null +++ b/spec/acceptance/reload_spec.rb @@ -0,0 +1,10 @@ +describe "reload" do + it "does not reset the value of use_parent_strategy" do + use_parent_strategy = :custom_use_parent_strategy_value + FactoryBot.use_parent_strategy = use_parent_strategy + + FactoryBot.reload + + expect(FactoryBot.use_parent_strategy).to eq use_parent_strategy + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 40c58e4..755d593 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,6 +20,7 @@ RSpec.configure do |config| config.before do FactoryBot.reload + FactoryBot.use_parent_strategy = false end config.after do