ibaralf
ibaralf

Reputation: 12528

FactoryBot get available traits for a factory

Is there a FactoryBot method or some way to get available traits for a factory?

Ex:

FactoryBot.define do
  factory :address, class: Address do
    trait :in_california do
      state 'CA'
    end

    trait :in_new_york do
      state 'NY'
    end

    trait :in_florida do
      state 'FL'
    end
end

I want to be able to get the traits programatically, something like FactoryBot.get_traits (:address) and it would return an array of the traits defined for that factory, in this case that would be

["in_california", "in_new_york", "in_florida"]

Does that make it clearer?

Upvotes: 9

Views: 1952

Answers (1)

Kees Briggs
Kees Briggs

Reputation: 381

I believe what you want is the following:

FactoryBot.factories[:address].defined_traits.map(&:name)
#=> ["in_california", "in_new_york", "in_florida"]

Upvotes: 11

Related Questions