linkyndy
linkyndy

Reputation: 17900

Generate 3-letter strings with FactoryGirl/Faker

I would like to generate random, unique strings matching this pattern: [A-Z]{3}. How can I achieve this with FactoryGirl or Faker?

I thought about FactoryGirl's sequences, but can't make it work.

Upvotes: 3

Views: 2073

Answers (3)

Artem P
Artem P

Reputation: 5333

There is option:

Faker::Name.initials(3)

Upvotes: 2

linkyndy
linkyndy

Reputation: 17900

I've ended up with:

sequence(:code) { ('A'..'Z').to_a.sample(3).join }

Upvotes: 6

victorkt
victorkt

Reputation: 14562

Try this:

FactoryGirl.define do
  sequence :str do |n|
    (0..2).map { (65 + rand(26)).chr }.join
  end
end

FactoryGirl.generate :str # => "GUW" 

Upvotes: 8

Related Questions