Reputation: 17900
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
Reputation: 17900
I've ended up with:
sequence(:code) { ('A'..'Z').to_a.sample(3).join }
Upvotes: 6
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