使用factory_girl编写工厂时,从数组中随机选择项目的正确方法是什么?

Joh*_*ohn 14 ruby-on-rails factory-bot

如果我在模型中的模块中定义了类型,并且在同一模型中也定义了这些类型的数组,那么在定义新工厂时如何随机调用此数组中的值?

谢谢!

约翰

tjw*_*ace 34

查看此答案 - 使用Array#sample:

FactoryGirl.define do
  factory :user do
    name "Foo Bar"
    foo { [:a, :list, :of, :values].sample }
  end
end
Run Code Online (Sandbox Code Playgroud)