1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Deprecate/Move strategies and callback methods

Why:
These methods are used internally for the functionality of the library
and are subject to change. Therefore shouldn't be part of the public
interface.

This PR:
- Moves the ```register_strategy```, ```register_callback```,
```register_default_factories```, ```register_default_callbacks```
```strategies```, ```callback_names```
and ```strategy_by_name``` methods to the ```FactoryBot::Internal```
class.
- Deprecates the use of ```register_callback``` from the ```FactoryBot```
module.
This commit is contained in:
Alejandro Dustet 2019-04-26 15:48:37 -04:00
parent 4d1cb6219b
commit f82e40c8c5
11 changed files with 130 additions and 42 deletions

View file

@ -88,11 +88,16 @@ module FactoryBot
to: :configuration to: :configuration
delegate :factory_by_name, delegate :factory_by_name,
:register_callback,
:register_default_callbacks,
:register_default_strategies,
:register_factory, :register_factory,
:register_sequence, :register_sequence,
:register_strategy,
:register_trait, :register_trait,
:rewind_sequences, :rewind_sequences,
:sequence_by_name, :sequence_by_name,
:strategy_by_name,
:trait_by_name, :trait_by_name,
to: Internal to: Internal
@ -100,45 +105,21 @@ module FactoryBot
deprecate :allow_class_lookup, deprecate :allow_class_lookup,
:allow_class_lookup=, :allow_class_lookup=,
:callback_names,
:factory_by_name, :factory_by_name,
:register_callback,
:register_default_callbacks,
:register_default_strategies,
:register_factory, :register_factory,
:register_trait, :register_trait,
:sequence_by_name, :sequence_by_name,
:sequences, :sequences,
:strategies,
:trait_by_name, :trait_by_name,
:traits, :traits,
deprecator: Deprecation deprecator: Deprecation
end end
def self.register_strategy(strategy_name, strategy_class)
strategies.register(strategy_name, strategy_class)
StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods
end end
def self.strategy_by_name(name) FactoryBot::Internal.register_default_strategies
strategies.find(name) FactoryBot::Internal.register_default_callbacks
end
def self.register_default_strategies
register_strategy(:build, FactoryBot::Strategy::Build)
register_strategy(:create, FactoryBot::Strategy::Create)
register_strategy(:attributes_for, FactoryBot::Strategy::AttributesFor)
register_strategy(:build_stubbed, FactoryBot::Strategy::Stub)
register_strategy(:null, FactoryBot::Strategy::Null)
end
def self.register_default_callbacks
register_callback(:after_build)
register_callback(:after_create)
register_callback(:after_stub)
register_callback(:before_create)
end
def self.register_callback(name)
name = name.to_sym
callback_names << name
end
end
FactoryBot.register_default_strategies
FactoryBot.register_default_callbacks

View file

@ -28,9 +28,9 @@ module FactoryBot
private private
def ensure_valid_callback_name! def ensure_valid_callback_name!
unless FactoryBot.callback_names.include?(name) unless FactoryBot::Internal.callback_names.include?(name)
raise InvalidCallbackNameError, "#{name} is not a valid callback name. " + raise InvalidCallbackNameError, "#{name} is not a valid callback name. " +
"Valid callback names are #{FactoryBot.callback_names.inspect}" "Valid callback names are #{FactoryBot::Internal.callback_names.inspect}"
end end
end end

View file

@ -95,7 +95,7 @@ module FactoryBot
def callback(*names, &block) def callback(*names, &block)
names.each do |name| names.each do |name|
FactoryBot.register_callback(name) FactoryBot::Internal.register_callback(name)
add_callback(Callback.new(name, block)) add_callback(Callback.new(name, block))
end end
end end

View file

@ -1,8 +1,26 @@
module FactoryBot module FactoryBot
# @api private # @api private
module Internal module Internal
DEFAULT_STRATEGIES = {
build: FactoryBot::Strategy::Build,
create: FactoryBot::Strategy::Create,
attributes_for: FactoryBot::Strategy::AttributesFor,
build_stubbed: FactoryBot::Strategy::Stub,
null: FactoryBot::Strategy::Null,
}.freeze
DEFAULT_CALLBACKS = [
:after_create, :after_build, :after_stub, :after_create
].freeze
class << self class << self
delegate :inline_sequences, :sequences, :traits, :factories, to: :configuration delegate :callback_names,
:factories,
:inline_sequences,
:sequences,
:strategies,
:traits,
to: :configuration
def configuration def configuration
@configuration ||= Configuration.new @configuration ||= Configuration.new
@ -57,6 +75,28 @@ module FactoryBot
def factory_by_name(name) def factory_by_name(name)
factories.find(name) factories.find(name)
end end
def register_strategy(strategy_name, strategy_class)
strategies.register(strategy_name, strategy_class)
StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods
end
def strategy_by_name(name)
strategies.find(name)
end
def register_default_strategies
DEFAULT_STRATEGIES.each(&method(:register_strategy))
end
def register_default_callbacks
DEFAULT_CALLBACKS.each(&method(:register_callback))
end
def register_callback(name)
name = name.to_sym
callback_names << name
end
end end
end end
end end

View file

@ -1,8 +1,8 @@
module FactoryBot module FactoryBot
def self.reload def self.reload
Internal.reset_configuration Internal.reset_configuration
register_default_strategies Internal.register_default_strategies
register_default_callbacks Internal.register_default_callbacks
find_definitions find_definitions
end end
end end

View file

@ -20,7 +20,7 @@ module FactoryBot
end end
def strategy_name_to_object def strategy_name_to_object
FactoryBot.strategy_by_name(@name_or_object) FactoryBot::Internal.strategy_by_name(@name_or_object)
end end
end end
end end

View file

@ -30,7 +30,7 @@ module FactoryBot
## # factory with traits and attribute override ## # factory with traits and attribute override
## build_stubbed_list(:user, 15, :admin, :male, name: "John Doe") ## build_stubbed_list(:user, 15, :admin, :male, name: "John Doe")
module Methods module Methods
# @!parse FactoryBot.register_default_strategies # @!parse FactoryBot::Internal.register_default_strategies
# @!method build(name, *traits_and_overrides, &block) # @!method build(name, *traits_and_overrides, &block)
# (see #strategy_method) # (see #strategy_method)
# Builds a registered factory by name. # Builds a registered factory by name.

View file

@ -26,7 +26,7 @@ describe FactoryBot::Callback do
end end
it "allows valid callback names to be assigned" do it "allows valid callback names to be assigned" do
FactoryBot.callback_names.each do |callback_name| FactoryBot::Internal.callback_names.each do |callback_name|
expect { FactoryBot::Callback.new(callback_name, -> {}) }. expect { FactoryBot::Callback.new(callback_name, -> {}) }.
to_not raise_error to_not raise_error
end end

View file

@ -96,4 +96,65 @@ describe FactoryBot::Internal do
expect(FactoryBot::Internal.factory_by_name(factory.name)).to eq factory expect(FactoryBot::Internal.factory_by_name(factory.name)).to eq factory
end end
end end
describe ".register_factory" do
it "registers the provided factory" do
factory = FactoryBot::Factory.new(:object)
configuration = FactoryBot::Internal.configuration
expect { FactoryBot::Internal.register_factory(factory) }.
to change { configuration.factories.count }.
from(0).
to(1)
end
it "returns the registered factory" do
factory = FactoryBot::Factory.new(:object)
expect(FactoryBot::Internal.register_factory(factory)).to eq factory
end
end
describe ".factory_by_name" do
it "finds a registered factory" do
factory = FactoryBot::Factory.new(:object)
FactoryBot::Internal.register_factory(factory)
expect(FactoryBot::Internal.factory_by_name(factory.name)).to eq factory
end
end
describe ".register_strategy" do
it "register the provided strategy name with the class" do
configuration = FactoryBot::Internal.configuration
initial_strategies_count = configuration.strategies.count
expect do
FactoryBot::Internal.register_strategy(:strategy_name, :strategy_class)
end.to change { configuration.strategies.count }.
from(initial_strategies_count).
to(initial_strategies_count + 1)
end
end
describe ".strategy_by_name" do
it "finds a registered strategy" do
FactoryBot::Internal.register_strategy(:strategy_name, :strategy_class)
expect(FactoryBot::Internal.strategy_by_name(:strategy_name)).
to eq :strategy_class
end
end
describe "default strategies and callbacks" do
FactoryBot::Internal::DEFAULT_STRATEGIES.
each do |strategy_name, strategy_class|
it "registers the #{strategy_name} strategy by default" do
expect(FactoryBot::Internal.strategy_by_name(strategy_name)).
to eq strategy_class
end
end
FactoryBot::Internal::DEFAULT_CALLBACKS.each do |callback_name|
it "registers the #{callback_name} by default" do
expect(FactoryBot::Internal.callback_names.include?(callback_name)).
to be_truthy
end
end
end
end end

View file

@ -13,14 +13,14 @@ describe FactoryBot::StrategyCalculator do
context "when a symbol" do context "when a symbol" do
before do before do
allow(FactoryBot).to receive(:strategy_by_name).and_return(strategy) allow(FactoryBot::Internal).to receive(:strategy_by_name).and_return(strategy)
end end
subject { FactoryBot::StrategyCalculator.new(:build).strategy } subject { FactoryBot::StrategyCalculator.new(:build).strategy }
it "finds the strategy by name" do it "finds the strategy by name" do
subject subject
expect(FactoryBot).to have_received(:strategy_by_name).with(:build) expect(FactoryBot::Internal).to have_received(:strategy_by_name).with(:build)
end end
it "returns the strategy found" do it "returns the strategy found" do

View file

@ -18,6 +18,12 @@ describe FactoryBot do
expect(FactoryBot.trait_by_name(trait.name)).to eq trait expect(FactoryBot.trait_by_name(trait.name)).to eq trait
end end
it "finds a registered strategy" do
FactoryBot.register_strategy(:strategy_name, :strategy_class)
expect(FactoryBot.strategy_by_name(:strategy_name)).
to eq :strategy_class
end
describe ".use_parent_strategy" do describe ".use_parent_strategy" do
it "is true by default" do it "is true by default" do
expect(FactoryBot.use_parent_strategy).to be true expect(FactoryBot.use_parent_strategy).to be true