In the test suite for my app, I use DatabaseCleaner to clear things out for each test. I’ve got a strange situation where one test fails roughly every other time I run the test suite, although that test passes when run individually. I’ve seen this occasionally with other tests in the suite.
Here’s the failure:
1) FactoryGirl creates a valid user record Failure/Error: user = FactoryGirl.create :user, :login => 'joe', :password => 'password', :email => 'email@example.com' ActiveRecord::RecordInvalid: Validation failed: Email has already been taken, Email has already been taken, Login has already been taken # ./spec/factories/factories_spec.rb:5:in `block (2 levels) in <top (required)>'
Here’s the test currently failing; I don’t think the problem is with this particular test.
require 'spec_helper' describe FactoryGirl do it "creates a valid user record" do user = FactoryGirl.create :user, :login => 'joe', :password => 'password', :email => 'firstname.lastname@example.org' expect(user.login).to eq 'joe' end end
Here’s my DatabaseCleaner setup, stored in ./spec/support/database_cleaner.rb. I’ve turned off several options trying to stabilize my tests:
require 'database_cleaner' RSpec.configure do |config| # From Avdi Grimm: http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/ config.before(:suite) do DatabaseCleaner.strategy = :truncation DatabaseCleaner.clean_with(:truncation) end config.before(:each) do #DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with :truncation end #config.before(:each, :js => true) do # DatabaseCleaner.strategy = :truncation #end #config.before(:each) do # DatabaseCleaner.start #end #config.after(:each) do # DatabaseCleaner.clean #end end
I also have config.use_transactional_fixtures = false in my spec_helper.rb file.
Any ideas as to what I’m doing wrong here?