608.620.5104 | info@tenforward.consulting

When testing in Rails, don't use the gem Faker for things that need to be unique

Published March 30, 2016

You'd think it unlikley that Faker::Internet.domain_word would return the same value twice, but when you're in a world of continuous integration where your tests get run all the time, it absolutely happens. (The same thing happens with Faker::Internet.email.) Faker has its uses, but not when you need unique values.

Instead, use a sequence, and thus guarantee that your tests won't randomly fail:

FactoryGirl.define do
factory :user do
sequence(:email) { |n| "email#{n}@example.com" }
end
end

Author details

Brian Samson

President
@samsonasu