2018-10-07 21:45:51 -04:00
|
|
|
require "set"
|
|
|
|
require "active_support/core_ext/module/delegation"
|
2019-01-02 21:16:23 -05:00
|
|
|
require "active_support/core_ext/module/attribute_accessors"
|
2018-10-07 21:45:51 -04:00
|
|
|
require "active_support/deprecation"
|
|
|
|
require "active_support/notifications"
|
2017-10-20 15:20:28 -04:00
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
require "factory_bot/definition_hierarchy"
|
|
|
|
require "factory_bot/configuration"
|
|
|
|
require "factory_bot/errors"
|
|
|
|
require "factory_bot/factory_runner"
|
|
|
|
require "factory_bot/strategy_syntax_method_registrar"
|
|
|
|
require "factory_bot/strategy_calculator"
|
|
|
|
require "factory_bot/strategy/build"
|
|
|
|
require "factory_bot/strategy/create"
|
|
|
|
require "factory_bot/strategy/attributes_for"
|
|
|
|
require "factory_bot/strategy/stub"
|
|
|
|
require "factory_bot/strategy/null"
|
|
|
|
require "factory_bot/registry"
|
|
|
|
require "factory_bot/null_factory"
|
|
|
|
require "factory_bot/null_object"
|
|
|
|
require "factory_bot/evaluation"
|
|
|
|
require "factory_bot/factory"
|
|
|
|
require "factory_bot/attribute_assigner"
|
|
|
|
require "factory_bot/evaluator"
|
|
|
|
require "factory_bot/evaluator_class_definer"
|
|
|
|
require "factory_bot/attribute"
|
|
|
|
require "factory_bot/callback"
|
|
|
|
require "factory_bot/callbacks_observer"
|
|
|
|
require "factory_bot/declaration_list"
|
|
|
|
require "factory_bot/declaration"
|
|
|
|
require "factory_bot/sequence"
|
|
|
|
require "factory_bot/attribute_list"
|
|
|
|
require "factory_bot/trait"
|
|
|
|
require "factory_bot/aliases"
|
|
|
|
require "factory_bot/definition"
|
|
|
|
require "factory_bot/definition_proxy"
|
|
|
|
require "factory_bot/syntax"
|
|
|
|
require "factory_bot/syntax_runner"
|
|
|
|
require "factory_bot/find_definitions"
|
|
|
|
require "factory_bot/reload"
|
|
|
|
require "factory_bot/decorator"
|
|
|
|
require "factory_bot/decorator/attribute_hash"
|
|
|
|
require "factory_bot/decorator/disallows_duplicates_registry"
|
|
|
|
require "factory_bot/decorator/invocation_tracker"
|
|
|
|
require "factory_bot/decorator/new_constructor"
|
|
|
|
require "factory_bot/linter"
|
|
|
|
require "factory_bot/version"
|
2019-02-11 19:16:56 -05:00
|
|
|
require "factory_bot/internal"
|
2017-10-20 15:20:28 -04:00
|
|
|
|
|
|
|
module FactoryBot
|
2019-04-19 16:01:32 -04:00
|
|
|
Deprecation = ActiveSupport::Deprecation.new("6.0", "factory_bot")
|
2019-01-05 21:19:04 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
def self.configuration
|
2019-02-11 19:16:56 -05:00
|
|
|
Internal.configuration
|
2017-10-20 15:20:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset_configuration
|
2019-02-11 19:16:56 -05:00
|
|
|
Internal.reset_configuration
|
2017-10-20 15:20:28 -04:00
|
|
|
end
|
|
|
|
|
2019-01-11 01:39:39 -05:00
|
|
|
mattr_accessor :use_parent_strategy, instance_accessor: false
|
|
|
|
self.use_parent_strategy = true
|
2019-01-02 21:16:23 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
# Look for errors in factories and (optionally) their traits.
|
|
|
|
# Parameters:
|
|
|
|
# factories - which factories to lint; omit for all factories
|
|
|
|
# options:
|
|
|
|
# traits: true - to lint traits as well as factories
|
|
|
|
# strategy: :create - to specify the strategy for linting
|
2018-11-21 15:28:28 -05:00
|
|
|
# verbose: true - to include full backtraces for each linting error
|
2017-10-20 15:20:28 -04:00
|
|
|
def self.lint(*args)
|
|
|
|
options = args.extract_options!
|
|
|
|
factories_to_lint = args[0] || FactoryBot.factories
|
2018-11-21 22:17:24 -05:00
|
|
|
Linter.new(factories_to_lint, options).lint!
|
2017-10-20 15:20:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
delegate :factories,
|
|
|
|
:sequences,
|
|
|
|
:traits,
|
|
|
|
:callbacks,
|
|
|
|
:strategies,
|
|
|
|
:callback_names,
|
|
|
|
:to_create,
|
|
|
|
:skip_create,
|
|
|
|
:initialize_with,
|
|
|
|
:constructor,
|
|
|
|
to: :configuration
|
2019-01-05 21:19:04 -05:00
|
|
|
|
2019-04-26 15:48:37 -04:00
|
|
|
delegate :register_sequence,
|
2019-04-19 16:01:32 -04:00
|
|
|
:register_trait,
|
2019-04-26 15:48:37 -04:00
|
|
|
:rewind_sequences,
|
|
|
|
:sequence_by_name,
|
|
|
|
:trait_by_name,
|
2019-04-19 16:01:32 -04:00
|
|
|
to: Internal
|
|
|
|
|
2019-01-05 21:19:04 -05:00
|
|
|
attr_accessor :allow_class_lookup
|
2019-04-19 16:01:32 -04:00
|
|
|
|
|
|
|
deprecate :allow_class_lookup,
|
|
|
|
:allow_class_lookup=,
|
|
|
|
:register_trait,
|
2019-04-26 15:48:37 -04:00
|
|
|
:sequence_by_name,
|
|
|
|
:sequences,
|
2019-04-19 16:01:32 -04:00
|
|
|
:trait_by_name,
|
|
|
|
:traits,
|
|
|
|
deprecator: Deprecation
|
2017-10-20 15:20:28 -04:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.strategy_by_name(name)
|
|
|
|
strategies.find(name)
|
|
|
|
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
|