Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create unique strings (with no numbers) with factory girl?

Or is an external gem necessary to generate random and unique usernames maybe?

Here's my current factory:

factory :user_4 do
    sequence(:id) { |n| n }
    sequence(:first_name) { |n| "Gemini" + n.to_s }
    sequence(:last_name) { |n| "Pollux" + n.to_s }
    sequence(:profile_name) { |n| "GeminiPollux" + n.to_s  }
    sequence(:email) { |n| "geminipollus" + n.to_s + "@hotmail.co.uk" }
end

Using the sequence method works for the id, profile_name and email, but my REGEX validations mean that the first name and last name are automatically invalid, because they have a digit in them. Nothing to do with the uniqueness.

So how should I create these unique names?

like image 332
Starkers Avatar asked Jan 03 '14 13:01

Starkers


1 Answers

factory :user_4 do
  # ...
  sequence(:first_name, 'a') { |n| "Gemini" + n }
  sequence(:last_name, 'a') { |n| "Pollux" + n }
  # ...
end

FactoryGirl's #sequence method accepts a second argument as its starting value. This can be a String, a Fixnum, or something else that supports being incremented by a #next method.

With a starting value of 'a' the sequence will be a, b, c ... z, aa, ab, ac ...

like image 137
tomc Avatar answered Sep 30 '22 06:09

tomc