Module: FactoryBot

Extended by:
Syntax::Default
Defined in:
lib/factory_bot.rb,
lib/factory_bot/enum.rb,
lib/factory_bot/trait.rb,
lib/factory_bot/errors.rb,
lib/factory_bot/linter.rb,
lib/factory_bot/reload.rb,
lib/factory_bot/syntax.rb,
lib/factory_bot/aliases.rb,
lib/factory_bot/factory.rb,
lib/factory_bot/version.rb,
lib/factory_bot/callback.rb,
lib/factory_bot/internal.rb,
lib/factory_bot/registry.rb,
lib/factory_bot/sequence.rb,
lib/factory_bot/attribute.rb,
lib/factory_bot/decorator.rb,
lib/factory_bot/evaluator.rb,
lib/factory_bot/definition.rb,
lib/factory_bot/evaluation.rb,
lib/factory_bot/declaration.rb,
lib/factory_bot/null_object.rb,
lib/factory_bot/null_factory.rb,
lib/factory_bot/configuration.rb,
lib/factory_bot/strategy/null.rb,
lib/factory_bot/strategy/stub.rb,
lib/factory_bot/syntax_runner.rb,
lib/factory_bot/attribute_list.rb,
lib/factory_bot/factory_runner.rb,
lib/factory_bot/strategy/build.rb,
lib/factory_bot/syntax/default.rb,
lib/factory_bot/syntax/methods.rb,
lib/factory_bot/strategy/create.rb,
lib/factory_bot/declaration_list.rb,
lib/factory_bot/definition_proxy.rb,
lib/factory_bot/find_definitions.rb,
lib/factory_bot/attribute/dynamic.rb,
lib/factory_bot/attribute/sequence.rb,
lib/factory_bot/attribute_assigner.rb,
lib/factory_bot/callbacks_observer.rb,
lib/factory_bot/declaration/dynamic.rb,
lib/factory_bot/strategy_calculator.rb,
lib/factory_bot/declaration/implicit.rb,
lib/factory_bot/definition_hierarchy.rb,
lib/factory_bot/attribute/association.rb,
lib/factory_bot/declaration/association.rb,
lib/factory_bot/evaluator_class_definer.rb,
lib/factory_bot/strategy/attributes_for.rb,
lib/factory_bot/decorator/attribute_hash.rb,
lib/factory_bot/decorator/new_constructor.rb,
lib/factory_bot/decorator/invocation_tracker.rb,
lib/factory_bot/strategy_syntax_method_registrar.rb,
lib/factory_bot/decorator/disallows_duplicates_registry.rb

Defined Under Namespace

Modules: Internal, Strategy, Syntax Classes: AssociationDefinitionError, Attribute, AttributeAssigner, AttributeDefinitionError, AttributeList, Callback, CallbacksObserver, Configuration, Declaration, DeclarationList, Decorator, Definition, DefinitionHierarchy, DefinitionProxy, DuplicateDefinitionError, Enum, Evaluation, Evaluator, EvaluatorClassDefiner, Factory, FactoryRunner, InvalidCallbackNameError, InvalidFactoryError, Linter, MethodDefinitionError, NullFactory, NullObject, Registry, Sequence, SequenceAbuseError, StrategyCalculator, StrategySyntaxMethodRegistrar, SyntaxRunner, Trait, TraitDefinitionError

Constant Summary collapse

Deprecation =
ActiveSupport::Deprecation.new("7.0", "factory_bot")
VERSION =
"6.5.0".freeze

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Syntax::Default

define, modify

Methods included from Syntax::Methods

#attributes_for, #attributes_for_list, #attributes_for_pair, #build, #build_list, #build_pair, #build_stubbed, #build_stubbed_list, #build_stubbed_pair, #create, #create_list, #create_pair, #generate, #generate_list

Class Attribute Details

.aliasesObject

Returns the value of attribute aliases.



3
4
5
# File 'lib/factory_bot/aliases.rb', line 3

def aliases
  @aliases
end

.definition_file_pathsObject

An Array of strings specifying locations that should be searched for factory definitions. By default, factory_bot will attempt to require “factories”, “test/factories” and “spec/factories”. Only the first existing file will be loaded.



7
8
9
# File 'lib/factory_bot/find_definitions.rb', line 7

def definition_file_paths
  @definition_file_paths
end

Class Method Details

.aliases_for(attribute) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/factory_bot/aliases.rb', line 11

def self.aliases_for(attribute)
  aliases.map { |(pattern, replace)|
    if pattern.match?(attribute)
      attribute.to_s.sub(pattern, replace).to_sym
    end
  }.compact << attribute
end

.build_stubbed_starting_id=(starting_id) ⇒ Object

Set the starting value for ids when using the build_stubbed strategy

Arguments:

  • starting_id Integer The new starting id value.



79
80
81
# File 'lib/factory_bot.rb', line 79

def self.build_stubbed_starting_id=(starting_id)
  Strategy::Stub.next_id = starting_id - 1
end

.find_definitionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/factory_bot/find_definitions.rb', line 12

def self.find_definitions
  absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) }

  absolute_definition_file_paths.uniq.each do |path|
    load("#{path}.rb") if File.exist?("#{path}.rb")

    if File.directory? path
      Dir[File.join(path, "**", "*.rb")].sort.each do |file|
        load file
      end
    end
  end
end

.lint(*args) ⇒ Object

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
verbose: true - to include full backtraces for each linting error


68
69
70
71
72
# File 'lib/factory_bot.rb', line 68

def self.lint(*args)
  options = args.extract_options!
  factories_to_lint = args[0] || FactoryBot.factories
  Linter.new(factories_to_lint, **options).lint!
end

.reloadObject



2
3
4
5
6
# File 'lib/factory_bot/reload.rb', line 2

def self.reload
  Internal.reset_configuration
  Internal.register_default_strategies
  find_definitions
end