How to set up database_cleaner for Rails with Cucumber and RSpec

Updated . Posted . Visible to the public.

Add gem 'database_cleaner' to your Gemfile. Then:

Cucumber & Rails 3+

# features/support/database_cleaner.rb

DatabaseCleaner.clean_with(:deletion) # clean once, now
DatabaseCleaner.strategy = :transaction
Cucumber::Rails::Database.javascript_strategy = :deletion

Cucumber & Rails 2

The latest available cucumber-rails for Rails 2 automatically uses database_cleaner when cucumber/rails/active_record is required -- but only if transactional fixtures are off. To have database_cleaner work correctly:

  1. Add the attached database_cleaner.rb to features/support/
  2. Make sure features/support/env.rb contains the following lines in this order:
# features/support/env.rb

require 'features/support/database_cleaner'
require 'cucumber/rails/active_record'
require 'cucumber/rails/world'
Cucumber::Rails::World.use_transactional_fixtures = false

RSpec

# spec/support/database_cleaner.rb

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:deletion)
  end

  config.around do |example|
    DatabaseCleaner.start
    example.run
    DatabaseCleaner.clean
  end
end
# spec/spec_helper.rb

RSpec.configure do |config|
  config.use_transactional_fixtures = false
end 

Important Don't use a combination of config.before and config.after here otherwise around hooks are not within the transaction and your database is therefore not cleaned properly between examples due to execution order, e.g.

around do |example|
  superuser_power = Power.new(create(:user, :superuser))
  Power.with_power(superuser_power) do
    # before-block will be run here, DB snapshot will be created
    example.run
    # after-block will be run here, restore of DB
  end
end

Comments

  • clean_with wipes the database just when it is called. The above configurations make sure the test database(s) are emptied up-front.
  • The :transaction strategy does not work with Selenium.

Also see Understanding database cleaning strategies in tests.

Dominik Schöler
Last edit
Daniel Straßner
Attachments
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2015-01-23 11:06)