mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
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:
parent
5947e10578
commit
4d1cb6219b
12 changed files with 87 additions and 38 deletions
|
@ -75,19 +75,21 @@ module FactoryBot
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
delegate :factories,
|
delegate :callbacks,
|
||||||
:sequences,
|
|
||||||
:traits,
|
|
||||||
:callbacks,
|
|
||||||
:strategies,
|
|
||||||
:callback_names,
|
:callback_names,
|
||||||
:to_create,
|
|
||||||
:skip_create,
|
|
||||||
:initialize_with,
|
|
||||||
:constructor,
|
:constructor,
|
||||||
|
:factories,
|
||||||
|
:initialize_with,
|
||||||
|
:sequences,
|
||||||
|
:skip_create,
|
||||||
|
:strategies,
|
||||||
|
:to_create,
|
||||||
|
:traits,
|
||||||
to: :configuration
|
to: :configuration
|
||||||
|
|
||||||
delegate :register_sequence,
|
delegate :factory_by_name,
|
||||||
|
:register_factory,
|
||||||
|
:register_sequence,
|
||||||
:register_trait,
|
:register_trait,
|
||||||
:rewind_sequences,
|
:rewind_sequences,
|
||||||
:sequence_by_name,
|
:sequence_by_name,
|
||||||
|
@ -98,6 +100,8 @@ module FactoryBot
|
||||||
|
|
||||||
deprecate :allow_class_lookup,
|
deprecate :allow_class_lookup,
|
||||||
:allow_class_lookup=,
|
:allow_class_lookup=,
|
||||||
|
:factory_by_name,
|
||||||
|
:register_factory,
|
||||||
:register_trait,
|
:register_trait,
|
||||||
:sequence_by_name,
|
:sequence_by_name,
|
||||||
:sequences,
|
:sequences,
|
||||||
|
@ -106,17 +110,6 @@ module FactoryBot
|
||||||
deprecator: Deprecation
|
deprecator: Deprecation
|
||||||
end
|
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)
|
def self.register_strategy(strategy_name, strategy_class)
|
||||||
strategies.register(strategy_name, strategy_class)
|
strategies.register(strategy_name, strategy_class)
|
||||||
StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods
|
StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods
|
||||||
|
|
|
@ -145,7 +145,7 @@ module FactoryBot
|
||||||
|
|
||||||
def parent
|
def parent
|
||||||
if @parent
|
if @parent
|
||||||
FactoryBot.factory_by_name(@parent)
|
FactoryBot::Internal.factory_by_name(@parent)
|
||||||
else
|
else
|
||||||
NullFactory.new
|
NullFactory.new
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,7 +9,7 @@ module FactoryBot
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(runner_strategy = @strategy, &block)
|
def run(runner_strategy = @strategy, &block)
|
||||||
factory = FactoryBot.factory_by_name(@name)
|
factory = FactoryBot::Internal.factory_by_name(@name)
|
||||||
|
|
||||||
factory.compile
|
factory.compile
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ module FactoryBot
|
||||||
# @api private
|
# @api private
|
||||||
module Internal
|
module Internal
|
||||||
class << self
|
class << self
|
||||||
delegate :inline_sequences, :sequences, :traits, to: :configuration
|
delegate :inline_sequences, :sequences, :traits, :factories, to: :configuration
|
||||||
|
|
||||||
def configuration
|
def configuration
|
||||||
@configuration ||= Configuration.new
|
@configuration ||= Configuration.new
|
||||||
|
@ -46,6 +46,17 @@ module FactoryBot
|
||||||
sequences.each(&:rewind)
|
sequences.each(&:rewind)
|
||||||
rewind_inline_sequences
|
rewind_inline_sequences
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,7 +17,7 @@ module FactoryBot
|
||||||
proxy = FactoryBot::DefinitionProxy.new(factory.definition)
|
proxy = FactoryBot::DefinitionProxy.new(factory.definition)
|
||||||
proxy.instance_eval(&block) if block_given?
|
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)|
|
proxy.child_factories.each do |(child_name, child_options, child_block)|
|
||||||
parent_factory = child_options.delete(:parent) || name
|
parent_factory = child_options.delete(:parent) || name
|
||||||
|
@ -60,7 +60,7 @@ module FactoryBot
|
||||||
|
|
||||||
class ModifyDSL
|
class ModifyDSL
|
||||||
def factory(name, _options = {}, &block)
|
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 = FactoryBot::DefinitionProxy.new(factory.definition.overridable)
|
||||||
proxy.instance_eval(&block)
|
proxy.instance_eval(&block)
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,8 +12,8 @@ unless ActiveSupport::Notifications.respond_to?(:subscribed)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "using ActiveSupport::Instrumentation to track factory interaction" do
|
describe "using ActiveSupport::Instrumentation to track factory interaction" do
|
||||||
let(:slow_user_factory) { FactoryBot.factory_by_name("slow_user") }
|
let(:slow_user_factory) { FactoryBot::Internal.factory_by_name("slow_user") }
|
||||||
let(:user_factory) { FactoryBot.factory_by_name("user") }
|
let(:user_factory) { FactoryBot::Internal.factory_by_name("user") }
|
||||||
before do
|
before do
|
||||||
define_model("User", email: :string)
|
define_model("User", email: :string)
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
|
|
|
@ -8,6 +8,7 @@ describe "an instance generated by a factory named a camel case string " do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "registers the UserModel factory" do
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,6 +8,7 @@ describe "an instance generated by a factory" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "registers the user factory" do
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,7 @@ describe FactoryBot::Factory do
|
||||||
@name = :user
|
@name = :user
|
||||||
@class = define_class("User")
|
@class = define_class("User")
|
||||||
@factory = FactoryBot::Factory.new(@name)
|
@factory = FactoryBot::Factory.new(@name)
|
||||||
FactoryBot.register_factory(@factory)
|
FactoryBot::Internal.register_factory(@factory)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "has a factory name" do
|
it "has a factory name" do
|
||||||
|
@ -16,7 +16,7 @@ describe FactoryBot::Factory do
|
||||||
|
|
||||||
it "returns associations" do
|
it "returns associations" do
|
||||||
factory = FactoryBot::Factory.new(:post)
|
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(:author, {}))
|
||||||
factory.declare_attribute(FactoryBot::Declaration::Association.new(:editor, {}))
|
factory.declare_attribute(FactoryBot::Declaration::Association.new(:editor, {}))
|
||||||
factory.declare_attribute(FactoryBot::Declaration::Implicit.new(:admin, factory))
|
factory.declare_attribute(FactoryBot::Declaration::Implicit.new(:admin, factory))
|
||||||
|
@ -32,7 +32,7 @@ describe FactoryBot::Factory do
|
||||||
|
|
||||||
factory = FactoryBot::Factory.new(:post)
|
factory = FactoryBot::Factory.new(:post)
|
||||||
factory.declare_attribute(association_on_parent)
|
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 = FactoryBot::Factory.new(:child_post, parent: :post)
|
||||||
child_factory.declare_attribute(association_on_child)
|
child_factory.declare_attribute(association_on_child)
|
||||||
|
|
|
@ -3,17 +3,25 @@ describe FactoryBot::Internal do
|
||||||
it "registers the provided trait" do
|
it "registers the provided trait" do
|
||||||
trait = FactoryBot::Trait.new(:admin)
|
trait = FactoryBot::Trait.new(:admin)
|
||||||
configuration = FactoryBot::Internal.configuration
|
configuration = FactoryBot::Internal.configuration
|
||||||
|
|
||||||
expect { FactoryBot::Internal.register_trait(trait) }.
|
expect { FactoryBot::Internal.register_trait(trait) }.
|
||||||
to change { configuration.traits.count }.
|
to change { configuration.traits.count }.
|
||||||
from(0).
|
from(0).
|
||||||
to(1)
|
to(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns the registered trait" do
|
||||||
|
trait = FactoryBot::Trait.new(:admin)
|
||||||
|
|
||||||
|
expect(FactoryBot::Internal.register_trait(trait)).to eq trait
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".trait_by_name" do
|
describe ".trait_by_name" do
|
||||||
it "finds a previously registered trait" do
|
it "finds a previously registered trait" do
|
||||||
trait = FactoryBot::Trait.new(:admin)
|
trait = FactoryBot::Trait.new(:admin)
|
||||||
FactoryBot::Internal.register_trait(trait)
|
FactoryBot::Internal.register_trait(trait)
|
||||||
|
|
||||||
expect(FactoryBot::Internal.trait_by_name(trait.name)).to eq trait
|
expect(FactoryBot::Internal.trait_by_name(trait.name)).to eq trait
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -22,17 +30,25 @@ describe FactoryBot::Internal do
|
||||||
it "registers the provided sequence" do
|
it "registers the provided sequence" do
|
||||||
sequence = FactoryBot::Sequence.new(:email)
|
sequence = FactoryBot::Sequence.new(:email)
|
||||||
configuration = FactoryBot::Internal.configuration
|
configuration = FactoryBot::Internal.configuration
|
||||||
|
|
||||||
expect { FactoryBot::Internal.register_sequence(sequence) }.
|
expect { FactoryBot::Internal.register_sequence(sequence) }.
|
||||||
to change { configuration.sequences.count }.
|
to change { configuration.sequences.count }.
|
||||||
from(0).
|
from(0).
|
||||||
to(1)
|
to(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns the registered sequence" do
|
||||||
|
sequence = FactoryBot::Sequence.new(:email)
|
||||||
|
|
||||||
|
expect(FactoryBot::Internal.register_sequence(sequence)).to eq sequence
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".sequence_by_name" do
|
describe ".sequence_by_name" do
|
||||||
it "finds a registered sequence" do
|
it "finds a registered sequence" do
|
||||||
sequence = FactoryBot::Sequence.new(:email)
|
sequence = FactoryBot::Sequence.new(:email)
|
||||||
FactoryBot::Internal.register_sequence(sequence)
|
FactoryBot::Internal.register_sequence(sequence)
|
||||||
|
|
||||||
expect(FactoryBot::Internal.sequence_by_name(sequence.name)).to eq sequence
|
expect(FactoryBot::Internal.sequence_by_name(sequence.name)).to eq sequence
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -53,4 +69,31 @@ describe FactoryBot::Internal do
|
||||||
expect(inline_sequence).to have_received(:rewind).exactly(:once)
|
expect(inline_sequence).to have_received(:rewind).exactly(:once)
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,7 @@ describe FactoryBot do
|
||||||
let(:sequence) { FactoryBot::Sequence.new(:email) }
|
let(:sequence) { FactoryBot::Sequence.new(:email) }
|
||||||
let(:trait) { FactoryBot::Trait.new(:admin) }
|
let(:trait) { FactoryBot::Trait.new(:admin) }
|
||||||
|
|
||||||
it "finds a registered factory" do
|
it "finds a registered factory", :silence_deprecation do
|
||||||
FactoryBot.register_factory(factory)
|
FactoryBot.register_factory(factory)
|
||||||
expect(FactoryBot.factory_by_name(factory.name)).to eq factory
|
expect(FactoryBot.factory_by_name(factory.name)).to eq factory
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ shared_examples_for "strategy without association support" do
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
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(:compile)
|
||||||
allow(factory).to receive(:run)
|
allow(factory).to receive(:run)
|
||||||
end
|
end
|
||||||
|
@ -27,7 +27,7 @@ shared_examples_for "strategy with association support" do |factory_bot_strategy
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
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(:compile)
|
||||||
allow(factory).to receive(:run)
|
allow(factory).to receive(:run)
|
||||||
end
|
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
|
it "finds the factory with the correct factory name" do
|
||||||
association_named(:author, factory_bot_strategy_name, great: "value")
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ shared_examples_for "strategy with strategy: :build" do |factory_bot_strategy_na
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
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(:compile)
|
||||||
allow(factory).to receive(:run)
|
allow(factory).to receive(:run)
|
||||||
end
|
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
|
it "finds the factory with the correct factory name" do
|
||||||
association_named(:author, strategy: :build, great: "value")
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue