diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index d5311f7..96ff509 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -48,7 +48,7 @@ require "factory_bot/version" require "factory_bot/internal" module FactoryBot - DEPRECATOR = ActiveSupport::Deprecation.new("6.0", "factory_bot") + Deprecation = ActiveSupport::Deprecation.new("6.0", "factory_bot") def self.configuration Internal.configuration @@ -87,8 +87,18 @@ module FactoryBot :constructor, to: :configuration + delegate :trait_by_name, + :register_trait, + to: Internal + attr_accessor :allow_class_lookup - deprecate :allow_class_lookup, :allow_class_lookup=, deprecator: DEPRECATOR + + deprecate :allow_class_lookup, + :allow_class_lookup=, + :register_trait, + :trait_by_name, + :traits, + deprecator: Deprecation end def self.register_factory(factory) @@ -118,17 +128,6 @@ module FactoryBot Internal.rewind_inline_sequences end - def self.register_trait(trait) - trait.names.each do |name| - traits.register(name, trait) - end - trait - end - - def self.trait_by_name(name) - traits.find(name) - end - def self.register_strategy(strategy_name, strategy_class) strategies.register(strategy_name, strategy_class) StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods diff --git a/lib/factory_bot/definition.rb b/lib/factory_bot/definition.rb index 9ca181f..e78d6bf 100644 --- a/lib/factory_bot/definition.rb +++ b/lib/factory_bot/definition.rb @@ -111,7 +111,7 @@ module FactoryBot end def trait_by_name(name) - trait_for(name) || FactoryBot.trait_by_name(name) + trait_for(name) || Internal.trait_by_name(name) end def trait_for(name) diff --git a/lib/factory_bot/internal.rb b/lib/factory_bot/internal.rb index 0e91b41..e0fea58 100644 --- a/lib/factory_bot/internal.rb +++ b/lib/factory_bot/internal.rb @@ -2,7 +2,7 @@ module FactoryBot # @api private module Internal class << self - delegate :inline_sequences, to: :configuration + delegate :inline_sequences, :traits, to: :configuration def configuration @configuration ||= Configuration.new @@ -19,6 +19,17 @@ module FactoryBot def rewind_inline_sequences inline_sequences.each(&:rewind) end + + def register_trait(trait) + trait.names.each do |name| + traits.register(name, trait) + end + trait + end + + def trait_by_name(name) + traits.find(name) + end end end end diff --git a/lib/factory_bot/syntax/default.rb b/lib/factory_bot/syntax/default.rb index c1381c4..9ad03cc 100644 --- a/lib/factory_bot/syntax/default.rb +++ b/lib/factory_bot/syntax/default.rb @@ -30,7 +30,7 @@ module FactoryBot end def trait(name, &block) - FactoryBot.register_trait(Trait.new(name, &block)) + Internal.register_trait(Trait.new(name, &block)) end def to_create(&block) diff --git a/spec/factory_bot/internal_spec.rb b/spec/factory_bot/internal_spec.rb new file mode 100644 index 0000000..f16464d --- /dev/null +++ b/spec/factory_bot/internal_spec.rb @@ -0,0 +1,20 @@ +describe FactoryBot::Internal do + describe ".register_trait" do + it "registers the provided trait" do + trait = FactoryBot::Trait.new(:admin) + configuration = FactoryBot::Internal.configuration + expect { FactoryBot::Internal.register_trait(trait) }. + to change { configuration.traits.count }. + from(0). + to(1) + end + end + + describe ".trait_by_name" do + it "finds a previously registered trait" do + trait = FactoryBot::Trait.new(:admin) + FactoryBot::Internal.register_trait(trait) + expect(FactoryBot::Internal.trait_by_name(trait.name)).to eq trait + end + end +end diff --git a/spec/factory_bot_spec.rb b/spec/factory_bot_spec.rb index 06e654c..5a41fa5 100644 --- a/spec/factory_bot_spec.rb +++ b/spec/factory_bot_spec.rb @@ -13,7 +13,7 @@ describe FactoryBot do expect(FactoryBot.sequence_by_name(sequence.name)).to eq sequence end - it "finds a registered trait" do + it "finds a registered trait", :silence_deprecation do FactoryBot.register_trait(trait) expect(FactoryBot.trait_by_name(trait.name)).to eq trait end diff --git a/spec/support/macros/deprecation.rb b/spec/support/macros/deprecation.rb index 3904e20..516b381 100644 --- a/spec/support/macros/deprecation.rb +++ b/spec/support/macros/deprecation.rb @@ -2,10 +2,10 @@ require "active_support" module SilenceDeprecation def silence_deprecation(example) - cached_silenced = ActiveSupport::Deprecation.silenced - ActiveSupport::Deprecation.silenced = true + cached_silenced = FactoryBot::Deprecation.silenced + FactoryBot::Deprecation.silenced = true example.run - ActiveSupport::Deprecation.silenced = cached_silenced + FactoryBot::Deprecation.silenced = cached_silenced end end