module FactoryBot

Constants

Deprecation
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 { |(pattern, replace)|
    if pattern.match?(attribute)
      attribute.to_s.sub(pattern, replace).to_sym
    end
  }.compact << attribute
end
build_stubbed_starting_id=(starting_id) click to toggle source

Set the starting value for ids when using the build_stubbed strategy

Arguments:

  • starting_id Integer The new starting id value.

# File lib/factory_bot.rb, line 78
def self.build_stubbed_starting_id=(starting_id)
  Strategy::Stub.next_id = starting_id - 1
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
verbose: true - to include full backtraces for each linting error
# File lib/factory_bot.rb, line 67
def self.lint(*args)
  options = args.extract_options!
  factories_to_lint = args[0] || FactoryBot.factories
  Linter.new(factories_to_lint, **options).lint!
end
reload() click to toggle source
# File lib/factory_bot/reload.rb, line 2
def self.reload
  Internal.reset_configuration
  Internal.register_default_strategies
  find_definitions
end