Search code examples
ruby-on-rails-3factory-bot

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


Suppose I have a model user, which has a uniqueness constraint on the email field

If I call Factory(:user) once all is well, but if I call it a second time it'll fail with an "entry already exists" error.

I'm currently using a simple helper to search for an existing entry in the DB before creating the factory...and calling any factory I make through that helper.

It works, but it's not entirely elegant, and considering how common I assume this problem must be, I'm guessing there's a better solution. So, is there an inbuilt way in factory girl to return_or_create a factory, instead of just charging ahead with create()? If not, how do most folk avoid duplicate entries with their factories?


Solution

  • 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(100000000).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]