1
votes

I'm having difficulty running multiple integration tests on Rails 4 (now called system tests in Rails 5).

Environment: Rails 4 / Minitest / Capybara / Poltergeist running on the Puma server.

  1. When I run a single test that creates a new record, it works every time.

    RAILS_ENV="test" ruby -I test test/integration/requests_test.rb -n /create_new/
    
  2. When I run the entire set of tests, the above test fails to create every time because the record already exists.

    RAILS_ENV="test" ruby -I test test/integration/requests_test.rb
    

I confirmed this by adding puts Request.all.collect(&:name) at the start - when running the group, the record being created already in the DB.

Here's the core issue - the DB is not reliably fresh for every test. (It is fresh for my unit tests and my functional tests, in groups and as individuals.) How can I make sure that my integration tests also start fresh each time?

In case it's helpful, the command above seems to be running Puma in development mode, even though I've specified ENV['RAILS_ENV'] = 'test' in test_helper.rb.

1

1 Answers

0
votes

Have you checked out the database_cleaner gem (https://github.com/DatabaseCleaner/database_cleaner)? You can use it to clear the database every time you run a new integration test in your RailsHelper config. I'd check out their documentation but mine looks like:

DatabaseCleaner.strategy = :truncation

RSpec.configure do |config|
 config.before(:each) do
  DatabaseCleaner.clean
 end

 config.after(:each) do
  FactoryGirl.reload #if you're using FactoryGirl
 end
end