1
votes

I have 2 integrations tests, here how it's look :

class TagFlowTest < ActionDispatch::IntegrationTest

  include Devise::Test::IntegrationHelpers

  setup do
    sign_in FactoryGirl.create(:admin)
    @partner = create(:partner)
  end

  test 'can see the list of partners' do
    get '/partners'
    assert_response :success
    assert_select 'tr', 2
  end 

... more tests below ...

The second test looks the same The thing is that when a launch rails test on this test, it's working fine. But when I launch :

rails test /test/

I got an error :

UnexpectedError: ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_email"

I understand that the issue come from

sign_in FactoryGirl.create(:admin)

When I delete this line on the other test, it's working. But if I do that, I cannot test one test only. How can I do the resolve this ?

1

1 Answers

3
votes
UnexpectedError: ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_email"

So what does this error mean? You have some validation in the model level or the db level which will not let you have the duplicate email for Admin.

So I presume that the factory you used to create Admin, is not creating unique email addresses.

Try this

FactoryGirl.define do
  factory :admin do
    # your code
    email { Faker::Internet.email }
  end
end

But this thing requires faker gem. If you don't want to use a gem just for this, try using sequences in factory girl.

Also it's good to have the database in a clean state, when running tests. Consider using https://github.com/DatabaseCleaner/database_cleaner