Skip to content

Instantly share code, notes, and snippets.

@mockdeep
Last active October 7, 2019 07:19
Show Gist options
  • Save mockdeep/9904695 to your computer and use it in GitHub Desktop.
Save mockdeep/9904695 to your computer and use it in GitHub Desktop.
A spec helper file for managing database cleaner configuration
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.treat_symbols_as_metadata_keys_with_true_values = true
# before everything
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
p cleaner_strategy
DatabaseCleaner.clean
end
config.prepend_before(:each, :type => :feature) do
DatabaseCleaner.strategy = :truncation
end
config.append_after(:each, :type => :feature) do
DatabaseCleaner.strategy = :transaction
end
end
def cleaner_strategy
active_record_cleaner.instance_variable_get(:@strategy).class
end
def active_record_cleaner
DatabaseCleaner.instance_variable_get(:@cleaners)[[:active_record, {}]]
end
@mockdeep
Copy link
Author

mockdeep commented Apr 1, 2014

Note, the various befores and afters don't seem to respect order in Rspec 1.3.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment