Rails: Avoiding duplication errors in Factory Girl...am I doing it wrong?

I found this a nice way to be sure the tests will always pass. Otherwise you can not be sure the 100% of the times you will create a unique email.

FactoryGirl.define do
  factory :user do
    name { Faker::Company.name }
    email { generate(:email) }
  end
  sequence(:email) do
    gen = "user_#{rand(1000)}@factory.com"
    while User.where(email: gen).exists?
      gen = "user_#{rand(1000)}@factory.com"
    end
    gen
  end
end

If you only need to generate a few values for attributes, you can also add a method to String, which keeps track of the prior strings used for an attribute. You could then do something like this:

factory :user do
  fullname { Faker::Name.name.unique('user_fullname') }
end

I use this approach for seeding. I wanted to avoid sequence numbers, because they do not look realistic.

Here the String extension which makes this happen:

class String
  # Makes sure that the current string instance is unique for the given id.
  # If you call unique multiple times on equivalent strings, this method will suffix it with a upcounting number.
  # Example:
  #     puts "abc".unique("some_attribute") #=> "abc"
  #     puts "abc".unique("some_attribute") #=> "abc-1"
  #     puts "abc".unique("some_attribute") #=> "abc-2"
  #     puts "abc".unique("other") #=> "abc"
  #
  # Internal: 
  #  We keep a data structure of the following format:
  #     @@unique_values = {
  #       "some_for_id" => { "used_string_1" : 1, "used_string_2": 2 } # the numbers represent the counter to be used as suffix for the next item
  #     }
  def unique(for_id)
    @@unique_values ||= {} # initialize structure in case this method was never called before
    @@unique_values[for_id] ||= {} # initialize structure in case we have not seen this id yet
    counter = @@unique_values[for_id][self] || 0
    result = (counter == 0) ? self : "#{self}-#{counter}"
    counter += 1
    @@unique_values[for_id][self] = counter
    return result
  end

end

Caution: This should not be used for lots of attributes, since we track all prior strings (optimizations possible).


Here's what I do to force the 'n' in my factory girl sequence to be the same as that object's id, and thereby avoid collisions:

First, I define a method that finds what the next id should be in app/models/user.rb:

def self.next_id
  self.last.nil? ? 1 : self.last.id + 1
end 

Then I call User.next_id from spec/factories.rb to start the sequence:

factory :user do
  association(:demo)
  association(:location)
  password  "password"
  sequence(:email, User.next_id) {|n| "darth_#{n}@sunni.ru" }
end

Simple answer: use factory.sequence

If you have a field that needs to be unique you can add a sequence in factory_girl to ensure that it is never the same:

Factory.define :user do |user|
  sequence(:email){|n| "user#{n}@factory.com" }
  user.password{ "secret" }
end

This will increment n each time in order to produce a unique email address such as [email protected]. (See https://github.com/thoughtbot/factory_girl/wiki/Usage for more info)

However this isn't always great in Rails.env.development...

Over time I have found that this is not actually the most useful way to create unique email addresses. The reason is that while the factory is always unique for your test environment it's not always unique for your development environment and n resets itself as you start the environment up and down. In :test this isn't a problem because the database is wiped but in :development you tend to keep the same data for a while.

You then get collisions and find yourself having to manually override the email to something you know is unique which is annoying.

Often more useful: use a random number

Since I call u = Factory :user from the console on a regular basis I go instead with generating a random number. You're not guaranteed to avoid collisions but in practice it hardly ever happens:

Factory.define :user do |user|
  user.email {"user_#{Random.rand(1000).to_s}@factory.com" }
  user.password{ "secret" }
end

N.B. You have to use Random.rand rather than rand() because of a collision (bug?) in FactoryGirl [https://github.com/thoughtbot/factory_girl/issues/219](see here).

This frees you to create users at will from the command line regardless of whether there are already factory generated users in the database.

Optional extra for making email testing easier

When you get into email testing you often want to verify that an action by a particular user triggered an email to another user.

You log in as Robin Hood, send an email to Maid Marion and then go to your inbox to verify it. What you see in your inbox is something from [email protected]. Who the hell is that?

You need to go back to your database to check whether the email was sent / received by whomever you expected it to be. Again this is a bit of a pain.

What I like to do instead is to generate the email using the name of the Factory user combined with a random number. This makes it far easier to check who things are coming from (and also makes collisions vanishingly unlikely). Using the Faker gem (http://faker.rubyforge.org/) to create the names we get:

Factory.define :user do |user|
  user.first_name { Faker::Name::first_name }
  user.last_name { Faker::Name::last_name }
  user.email {|u| "#{u.first_name}_#{u.last_name}_#{Random.rand(1000).to_s}@factory.com" }
end

finally, since Faker sometimes generates names that aren't email-friendly (Mike O'Donnell) we need to whitelist acceptable characters: .gsub(/[^a-zA-Z1-10]/, '')

Factory.define :user do |user|
  user.first_name { Faker::Name::first_name }
  user.last_name { Faker::Name::last_name }
  user.email {|u| "#{u.first_name.gsub(/[^a-zA-Z1-10]/, '')}_#{u.last_name.gsub(/[^a-zA-Z1-10]/, '')}_#{Random.rand(1000).to_s}@factory.com" }
end

This gives us personable but unique emails such as [email protected] and [email protected]