module FactoryBot

Constants

VERSION

Attributes

aliases[RW]
definition_file_paths[RW]

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.

Public Class Methods

aliases_for(attribute) click to toggle source
# File lib/factory_bot/aliases.rb, line 11
def self.aliases_for(attribute)
  aliases.map do |(pattern, replace)|
    if pattern.match(attribute.to_s)
      attribute.to_s.sub(pattern, replace).to_sym
    end
  end.compact << attribute
end
configuration() click to toggle source
# File lib/factory_bot.rb, line 50
def self.configuration
  @configuration ||= Configuration.new
end
factory_by_name(name) click to toggle source
# File lib/factory_bot.rb, line 99
def self.factory_by_name(name)
  factories.find(name)
end
find_definitions() click to toggle source
# 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) click to toggle source

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
# File lib/factory_bot.rb, line 64
def self.lint(*args)
  options = args.extract_options!
  factories_to_lint = args[0] || FactoryBot.factories
  linting_strategy = options[:traits] ? :factory_and_traits : :factory
  factory_strategy = options[:strategy] || :create
  Linter.new(factories_to_lint, linting_strategy, factory_strategy).lint!
end
register_callback(name) click to toggle source
# File lib/factory_bot.rb, line 153
def self.register_callback(name)
  name = name.to_sym
  callback_names << name
end
register_default_callbacks() click to toggle source
# File lib/factory_bot.rb, line 146
def self.register_default_callbacks
  register_callback(:after_build)
  register_callback(:after_create)
  register_callback(:after_stub)
  register_callback(:before_create)
end
register_default_strategies() click to toggle source
# File lib/factory_bot.rb, line 138
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
register_factory(factory) click to toggle source
# File lib/factory_bot.rb, line 92
def self.register_factory(factory)
  factory.names.each do |name|
    factories.register(name, factory)
  end
  factory
end
register_sequence(sequence) click to toggle source
# File lib/factory_bot.rb, line 103
def self.register_sequence(sequence)
  sequence.names.each do |name|
    sequences.register(name, sequence)
  end
  sequence
end
register_strategy(strategy_name, strategy_class) click to toggle source
# File lib/factory_bot.rb, line 129
def self.register_strategy(strategy_name, strategy_class)
  strategies.register(strategy_name, strategy_class)
  StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods
end
register_trait(trait) click to toggle source
# File lib/factory_bot.rb, line 118
def self.register_trait(trait)
  trait.names.each do |name|
    traits.register(name, trait)
  end
  trait
end
reload() click to toggle source
# File lib/factory_bot/reload.rb, line 2
def self.reload
  reset_configuration
  register_default_strategies
  register_default_callbacks
  find_definitions
end
reset_configuration() click to toggle source
# File lib/factory_bot.rb, line 54
def self.reset_configuration
  @configuration = nil
end
rewind_sequences() click to toggle source
# File lib/factory_bot.rb, line 114
def self.rewind_sequences
  sequences.each(&:rewind)
end
sequence_by_name(name) click to toggle source
# File lib/factory_bot.rb, line 110
def self.sequence_by_name(name)
  sequences.find(name)
end
strategy_by_name(name) click to toggle source
# File lib/factory_bot.rb, line 134
def self.strategy_by_name(name)
  strategies.find(name)
end
trait_by_name(name) click to toggle source
# File lib/factory_bot.rb, line 125
def self.trait_by_name(name)
  traits.find(name)
end