Deprecate and move to Internal factories 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_factory``` and ```factory_by_name``` methods to
the ```FactoryBot::Internal``` class.
- Deprecates the use of ```register_factory``` and ```factory_by_name```
from the ```FactoryBot``` module.
- Improve formatting of the specs
This commit is contained in:
Alejandro Dustet 2019-04-26 15:48:37 -04:00
parent 5947e10578
commit 4d1cb6219b
12 changed files with 87 additions and 38 deletions

View File

@ -75,19 +75,21 @@ module FactoryBot
end
class << self
delegate :factories,
:sequences,
:traits,
:callbacks,
:strategies,
delegate :callbacks,
:callback_names,
:to_create,
:skip_create,
:initialize_with,
:constructor,
:factories,
:initialize_with,
:sequences,
:skip_create,
:strategies,
:to_create,
:traits,
to: :configuration
delegate :register_sequence,
delegate :factory_by_name,
:register_factory,
:register_sequence,
:register_trait,
:rewind_sequences,
:sequence_by_name,
@ -98,6 +100,8 @@ module FactoryBot
deprecate :allow_class_lookup,
:allow_class_lookup=,
:factory_by_name,
:register_factory,
:register_trait,
:sequence_by_name,
:sequences,
@ -106,17 +110,6 @@ module FactoryBot
deprecator: Deprecation
end
def self.register_factory(factory)
factory.names.each do |name|
factories.register(name, factory)
end
factory
end
def self.factory_by_name(name)
factories.find(name)
end
def self.register_strategy(strategy_name, strategy_class)
strategies.register(strategy_name, strategy_class)
StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods

View File

@ -145,7 +145,7 @@ module FactoryBot
def parent
if @parent
FactoryBot.factory_by_name(@parent)
FactoryBot::Internal.factory_by_name(@parent)
else
NullFactory.new
end

View File

@ -9,7 +9,7 @@ module FactoryBot
end
def run(runner_strategy = @strategy, &block)
factory = FactoryBot.factory_by_name(@name)
factory = FactoryBot::Internal.factory_by_name(@name)
factory.compile

View File

@ -2,7 +2,7 @@ module FactoryBot
# @api private
module Internal
class << self
delegate :inline_sequences, :sequences, :traits, to: :configuration
delegate :inline_sequences, :sequences, :traits, :factories, to: :configuration
def configuration
@configuration ||= Configuration.new
@ -46,6 +46,17 @@ module FactoryBot
sequences.each(&:rewind)
rewind_inline_sequences
end
def register_factory(factory)
factory.names.each do |name|
factories.register(name, factory)
end
factory
end
def factory_by_name(name)
factories.find(name)
end
end
end
end

View File

@ -17,7 +17,7 @@ module FactoryBot
proxy = FactoryBot::DefinitionProxy.new(factory.definition)
proxy.instance_eval(&block) if block_given?
FactoryBot.register_factory(factory)
Internal.register_factory(factory)
proxy.child_factories.each do |(child_name, child_options, child_block)|
parent_factory = child_options.delete(:parent) || name
@ -60,7 +60,7 @@ module FactoryBot
class ModifyDSL
def factory(name, _options = {}, &block)
factory = FactoryBot.factory_by_name(name)
factory = Internal.factory_by_name(name)
proxy = FactoryBot::DefinitionProxy.new(factory.definition.overridable)
proxy.instance_eval(&block)
end

View File

@ -12,8 +12,8 @@ unless ActiveSupport::Notifications.respond_to?(:subscribed)
end
describe "using ActiveSupport::Instrumentation to track factory interaction" do
let(:slow_user_factory) { FactoryBot.factory_by_name("slow_user") }
let(:user_factory) { FactoryBot.factory_by_name("user") }
let(:slow_user_factory) { FactoryBot::Internal.factory_by_name("slow_user") }
let(:user_factory) { FactoryBot::Internal.factory_by_name("user") }
before do
define_model("User", email: :string)
FactoryBot.define do

View File

@ -8,6 +8,7 @@ describe "an instance generated by a factory named a camel case string " do
end
it "registers the UserModel factory" do
expect(FactoryBot.factory_by_name("UserModel")).to be_a(FactoryBot::Factory)
expect(FactoryBot::Internal.factory_by_name("UserModel")).
to be_a(FactoryBot::Factory)
end
end

View File

@ -8,6 +8,7 @@ describe "an instance generated by a factory" do
end
it "registers the user factory" do
expect(FactoryBot.factory_by_name(:user)).to be_a(FactoryBot::Factory)
expect(FactoryBot::Internal.factory_by_name(:user)).
to be_a(FactoryBot::Factory)
end
end

View File

@ -3,7 +3,7 @@ describe FactoryBot::Factory do
@name = :user
@class = define_class("User")
@factory = FactoryBot::Factory.new(@name)
FactoryBot.register_factory(@factory)
FactoryBot::Internal.register_factory(@factory)
end
it "has a factory name" do
@ -16,7 +16,7 @@ describe FactoryBot::Factory do
it "returns associations" do
factory = FactoryBot::Factory.new(:post)
FactoryBot.register_factory(FactoryBot::Factory.new(:admin))
FactoryBot::Internal.register_factory(FactoryBot::Factory.new(:admin))
factory.declare_attribute(FactoryBot::Declaration::Association.new(:author, {}))
factory.declare_attribute(FactoryBot::Declaration::Association.new(:editor, {}))
factory.declare_attribute(FactoryBot::Declaration::Implicit.new(:admin, factory))
@ -32,7 +32,7 @@ describe FactoryBot::Factory do
factory = FactoryBot::Factory.new(:post)
factory.declare_attribute(association_on_parent)
FactoryBot.register_factory(factory)
FactoryBot::Internal.register_factory(factory)
child_factory = FactoryBot::Factory.new(:child_post, parent: :post)
child_factory.declare_attribute(association_on_child)

View File

@ -3,17 +3,25 @@ describe FactoryBot::Internal 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
it "returns the registered trait" do
trait = FactoryBot::Trait.new(:admin)
expect(FactoryBot::Internal.register_trait(trait)).to eq trait
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
@ -22,17 +30,25 @@ describe FactoryBot::Internal do
it "registers the provided sequence" do
sequence = FactoryBot::Sequence.new(:email)
configuration = FactoryBot::Internal.configuration
expect { FactoryBot::Internal.register_sequence(sequence) }.
to change { configuration.sequences.count }.
from(0).
to(1)
end
it "returns the registered sequence" do
sequence = FactoryBot::Sequence.new(:email)
expect(FactoryBot::Internal.register_sequence(sequence)).to eq sequence
end
end
describe ".sequence_by_name" do
it "finds a registered sequence" do
sequence = FactoryBot::Sequence.new(:email)
FactoryBot::Internal.register_sequence(sequence)
expect(FactoryBot::Internal.sequence_by_name(sequence.name)).to eq sequence
end
end
@ -53,4 +69,31 @@ describe FactoryBot::Internal do
expect(inline_sequence).to have_received(:rewind).exactly(:once)
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
end

View File

@ -3,7 +3,7 @@ describe FactoryBot do
let(:sequence) { FactoryBot::Sequence.new(:email) }
let(:trait) { FactoryBot::Trait.new(:admin) }
it "finds a registered factory" do
it "finds a registered factory", :silence_deprecation do
FactoryBot.register_factory(factory)
expect(FactoryBot.factory_by_name(factory.name)).to eq factory
end

View File

@ -8,7 +8,7 @@ shared_examples_for "strategy without association support" do
end
before do
allow(FactoryBot).to receive(:factory_by_name).and_return factory
allow(FactoryBot::Internal).to receive(:factory_by_name).and_return factory
allow(factory).to receive(:compile)
allow(factory).to receive(:run)
end
@ -27,7 +27,7 @@ shared_examples_for "strategy with association support" do |factory_bot_strategy
end
before do
allow(FactoryBot).to receive(:factory_by_name).and_return factory
allow(FactoryBot::Internal).to receive(:factory_by_name).and_return factory
allow(factory).to receive(:compile)
allow(factory).to receive(:run)
end
@ -39,7 +39,7 @@ shared_examples_for "strategy with association support" do |factory_bot_strategy
it "finds the factory with the correct factory name" do
association_named(:author, factory_bot_strategy_name, great: "value")
expect(FactoryBot).to have_received(:factory_by_name).with(:author)
expect(FactoryBot::Internal).to have_received(:factory_by_name).with(:author)
end
end
@ -52,7 +52,7 @@ shared_examples_for "strategy with strategy: :build" do |factory_bot_strategy_na
end
before do
allow(FactoryBot).to receive(:factory_by_name).and_return factory
allow(FactoryBot::Internal).to receive(:factory_by_name).and_return factory
allow(factory).to receive(:compile)
allow(factory).to receive(:run)
end
@ -64,7 +64,7 @@ shared_examples_for "strategy with strategy: :build" do |factory_bot_strategy_na
it "finds the factory with the correct factory name" do
association_named(:author, strategy: :build, great: "value")
expect(FactoryBot).to have_received(:factory_by_name).with(:author)
expect(FactoryBot::Internal).to have_received(:factory_by_name).with(:author)
end
end