Search code examples
ruby-on-railsfactory-bot

Provide a whitelist of words in FactoryBot


I have a factory definition that looks like this:

FactoryBot.define do
  factory :foo do
    name { Faker::Lorem.word }
  end
end

Can I provide a custom list of words, so that the name is always going to be one of the words I choose?


Solution

  • If you have a different set of words than what Faker::Lorem might offer then you could simply use that;

    name { ['my', 'words', ...].sample }