Method: FactoryBot::DefinitionProxy#method_missing

Defined in:
lib/factory_bot/definition_proxy.rb

#method_missing(name, *args, &block) ⇒ Object

Calls add_attribute using the missing method name as the name of the attribute, so that:

factory :user do
  name { 'Billy Idol' }
end

and:

factory :user do
  add_attribute(:name) { 'Billy Idol' }
end

are equivalent.

If no argument or block is given, factory_bot will first look for an association, then for a sequence, and finally for a trait with the same name. This means that given an “admin” trait, an “email” sequence, and an “account” factory:

factory :user, traits: [:admin] do
  email { generate(:email) }
  association :account
end

and:

factory :user do
  admin
  email
  
end

are equivalent.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/factory_bot/definition_proxy.rb', line 91

def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
  association_options = args.first

  if association_options.nil?
    __declare_attribute__(name, block)
  elsif __valid_association_options?(association_options)
    association(name, association_options)
  else
    raise NoMethodError.new(<<~MSG)
      undefined method '#{name}' in '#{@definition.name}' factory
      Did you mean? '#{name} { #{association_options.inspect} }'
    MSG
  end
end