Skip to content

Instantly share code, notes, and snippets.

@maschwenk
Last active February 6, 2019 05:11
Show Gist options
  • Save maschwenk/59d7ea7d8a40bec6621f47723cec602d to your computer and use it in GitHub Desktop.
Save maschwenk/59d7ea7d8a40bec6621f47723cec602d to your computer and use it in GitHub Desktop.
Issue with Faker in specs

Problem

# Factory uses Faker::Name.first_name under the hoode -- evaluates to something with 'frank' as a substring
# Now you have two users named frank without realizing it
let(:user) { create :user } 
let(:another_user) { create :user, name: 'frank' }

it do
  filter_for_frank 
  expect(page).to have_text 'frank'
end

Solution

This is a simple illustration of one way Faker can be problematic in specs.

The basic issue is that you can't rely on Faker data on being unique unless you call Faker::{Generator}.unique.{method}.

If you want to make these kinds of expectations, be explicit about all the data under test and leave less responsibility to Faker:

let(:user) { create :user, name: 'alice' } 
let(:another_user) { create :user, name: 'frank' }

it do
  filter_for_frank 
  expect(page).to have_text 'frank'
end

Faker has more place in seed data than it does specs. Don't use Faker to expand your test coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment