Method: FactoryBot::DefinitionProxy#traits_for_enum

Defined in:
lib/factory_bot/definition_proxy.rb

#traits_for_enum(attribute_name, values = nil) ⇒ Object

Creates traits for enumerable values.

Example:

factory :task do
  traits_for_enum :status, [:started, :finished]
end

Equivalent to:

factory :task do
  trait :started do
    status { :started }
  end

  trait :finished do
    status { :finished }
  end
end

Example:

factory :task do
  traits_for_enum :status, {started: 1, finished: 2}
end

Example:

class Task
  def statuses
    {started: 1, finished: 2}
  end
end

factory :task do
  traits_for_enum :status
end

Both equivalent to:

factory :task do
  trait :started do
    status { 1 }
  end

  trait :finished do
    status { 2 }
  end
end

Arguments:

attribute_name: +Symbol+ or +String+
  the name of the attribute these traits will set the value of
values: +Array+, +Hash+, or other +Enumerable+
  An array of trait names, or a mapping of trait names to values for
  those traits. When this argument is not provided, factory_bot will
  attempt to get the values by calling the pluralized `attribute_name`
  class method.


237
238
239
# File 'lib/factory_bot/definition_proxy.rb', line 237

def traits_for_enum(attribute_name, values = nil)
  @definition.register_enum(Enum.new(attribute_name, values))
end