FactoryBot获取工厂的可用特征

时间:2018-07-27 18:50:32

标签: ruby-on-rails ruby factory-bot factory

是否存在FactoryBot方法或某种方式来获取工厂的可用特征?

例如:

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

我希望能够以编程方式获取特征,例如FactoryBot.get_traits(:address),它将返回为该工厂定义的特征数组,在这种情况下为

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

是否更清楚?

1 个答案:

答案 0 :(得分:3)

我相信您想要的是以下内容:

FactoryBot.factories[:address].defined_traits