Skip to content

Instantly share code, notes, and snippets.

@mcls
Last active December 16, 2016 11:29
Show Gist options
  • Save mcls/f975a73aa8bc0ab0299727bf4f83c930 to your computer and use it in GitHub Desktop.
Save mcls/f975a73aa8bc0ab0299727bf4f83c930 to your computer and use it in GitHub Desktop.
DatabaseCleaner gem setup for RSpec
# file: spec/support/database_cleaner.rb
require 'database_cleaner'
# More info: https://github.com/DatabaseCleaner/database_cleaner
RSpec.configure do |config|
config.before(:suite) do
# Ensure a clean slate
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
if example.metadata[:js]
# JS-based tests use multiple processes, so we can't use database
# transactions there.
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.strategy = :transaction
end
DatabaseCleaner.start
example.run
DatabaseCleaner.clean
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment