mercredi 8 juillet 2020

FactoryBot on model with unique validation ... can't rerun tests

I'm using FactoryBot for the first time and am having an issue with testing if it can create a valid user.

My test/factories/users.rb file is:

FactoryBot.define do
  factory :user do
    sequence(:email) { |n| "email#{n}@email.com" }
    sequence(:username) { |n| "first-name#{n} last-name#{n}" }
    sequence(:password) { |n| "password#{n}" }
  end
end

My test is:

it 'should test that the User factory is valid' do
    expect(FactoryBot.build :user).to be_valid
end

I'm using devise for my User model, and I am getting the following error the second time I run my test:

Validation failed: Email has already been taken

Not sure how to proceed from here.

Aucun commentaire:

Enregistrer un commentaire