ruby-on-railsrspecdevisecapybaraacts-as-paranoid

When I add rspec test, other tests start failing


When I add this test, and run the whole suite of tests, one other test fails. It's a different one every time, but the error is always the same.

I am using Rails, with Devise and Paranoia (which is what I'm testing, for soft-deleting).

This is the error:

SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("last_sign_in_at", "current_sign_in_at", "last_sign_in_ip", "current_sign_in_ip", "sign_in_count") VALUES (?, ?, ?, ?, ?)

This is the test that makes other tests fail.

require "rails_helper"

feature "User soft deletes project" do
  scenario "successfully" do
    user = create(:user)
    login_as(user, scope: :user)
    project = create(:project, creator: user)
    visit edit_user_project_path(user, project)

    expect {
      click_on "Delete Project"
    }.to change(Project, :count).by(-1)

    expect(page).to have_content("Project was successfully destroyed")
    expect(Project.unscoped.count).to eq(1)
  end
end

Solution

  • Seems like you are not clearing the database in between each test suite. For example, you create a user for your first test suite. In your next test suite, you create a user again, with the same email, which is supposed to be unique, and you get that error. Add the database_cleaner gem to your Gemfile, run bundle install and add this to spec_helper.rb

    config.before(:suite) do
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end