Last active
January 18, 2016 23:41
-
-
Save andreypronin/4950750 to your computer and use it in GitHub Desktop.
RSpec hooks I use with DatabaseCleaner. We can't use transactional fixtures with Selenium/Poltergeist, but it's an overkill to truncate/delete for non-JS specs. Thus this optimization here. Read more at http://railscasts.com/episodes/257-request-specs-and-capybara. Assumption (just in case): only javascript_driver is set to Poltergeist/Selenium …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.use_transactional_fixtures = false | |
config.before(:suite) do | |
# Do truncation once per suite to vacuum for Postgres | |
DatabaseCleaner.clean_with :truncation | |
# Normally do transactions-based cleanup | |
DatabaseCleaner.strategy = :transaction | |
end | |
config.around(:each) do |spec| | |
if spec.metadata[:js] || spec.metadata[:test_commit] | |
# JS => run with PhantomJS that doesn't share connections => can't use transactions | |
# deletion is often faster than truncation on Postgres - doesn't vacuum | |
# no need to 'start', clean_with is sufficient for deletion | |
# Devise Emails: devise-async sends confirmation on commit only! => can't use transactions | |
spec.run | |
DatabaseCleaner.clean_with :deletion | |
else | |
# No JS/Devise => run with Rack::Test => transactions are ok | |
DatabaseCleaner.start | |
spec.run | |
DatabaseCleaner.clean | |
# see https://github.com/bmabey/database_cleaner/issues/99 | |
begin | |
ActiveRecord::Base.connection.send(:rollback_transaction_records, true) | |
rescue | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this, gave me a nice 15-20 second speed boost on my small test suite. :)