Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to select an item randomly from an array when writing factories with factory_girl?

If I have types defined in a module inside a model, and an array of these types also defined in the same model, how can I call a value from this array randomly when defining a new factory?

Thanks!

John

like image 355
John Avatar asked Nov 22 '11 06:11

John


Video Answer


1 Answers

Checkout this answer - use Array#sample:

FactoryGirl.define do
  factory :user do
    name "Foo Bar"
    foo { [:a, :list, :of, :values].sample }
  end
end
like image 117
tjwallace Avatar answered Oct 05 '22 23:10

tjwallace