Search code examples
mysqlruby-on-railsrubyrspecdatabase-cleaner

DatabaseCleaner not working with Capybara automated tests


I'm running automated tests using Capybara and Poltergeist on a Rails site that runs on MySQL.

I see where it commits the "data" inserts when running this automated suite but DatabaseCleaner seems to do nothing, I get no errors when running it at all either.

(I made sure gems are installed as well). This is my first run with DatabaseCleaner, but nothing seems to be working.

Relevant portion of rails_helper file:

RSpec.configure do |config|

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

     config.before(:each) do
          DatabaseCleaner.strategy = :transaction
     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

Also, I made sure config.use_transactional_fixtures = false.

My test_helper.rb file is nothing fancy either:

#Load up Capybara/Rspec
require 'rails_helper'
require 'rspec'
require 'capybara/rspec'
require 'capybara'
require 'capybara/dsl'    

#Load up Poltergeist
require 'capybara/poltergeist'

#Set JS Supported Driver
Capybara.javascript_driver = :poltergeist

And finally a sample script, which basically goes through a locally run Rails website and creates a record. It's simple, but I'm just using this as an example. The automated tests all do their thing.

require 'test_helper'
describe  "Test", :type => :feature, :js => true do
  it 'Add Widget' do
    visit ('/')
    fill_in('user_email', :with => '[email protected]')
    fill_in('user_password', :with => 'p@ssword')
    click_button('Log in')
    click_link('Tools')
    expect(page).to have_content 'Tools'
    click_link('Stuff')
    expect(page).to have_content 'Stuff'
    click_link('Create New Thingy')
    expect(page).to have_content 'New Thingy'
    fill_in('thingy_name', :with => 'Name for a Thing!')
    click_button('Create Thing!')
  end
end

Am I missing something here? I don't get any errors when running, but the data just doesn't get deleted.

I'm running MySQL 5.6.26 and Rails 4.0.2.


Solution

  • By default, database_cleaner will clean the test database for the app whose tests are being run. If the requests in your integration spec are going to some other app, not running against this test database, then it will do nothing to that other database (it doesn't even know it exists).

    You can give database cleaner extra databases to clean. If your database.yml listed the details for this other db with the key :integration, ie it contained something like

    integration:
      adapter: mysql
      database: some_db
      ...
    

    Then you could use

    DatabaseCleaner[:active_record, { :connection => :integration }].strategy = :truncation
    

    To specify that this database should also be truncated when calling DatabaseCleaner.clean. This will of course delete everything from that database on each test run.