Javi, thanks! Had to figure it our myself, only to discover later that you have this snippet here for over 2 years. I went a bit further with optimization, so my code is as follows:
ruby
config.before(:suite) do# Do truncation once per suite to vacuum for PostgresDatabaseCleaner.clean_with :truncation# Normally do transactions-based cleanupDatabaseCleaner.strategy = :transactionend
config.around(:each) do |spec|
if spec.metadata[:js]
# JS => run with Poltergeist/Selenium 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
spec.run
DatabaseCleaner.clean_with :deletionelse# No JS => run with Rack::Test => transactions are okDatabaseCleaner.start
spec.run
DatabaseCleaner.clean
endend
Javi, thanks! Had to figure it our myself, only to discover later that you have this snippet here for over 2 years. I went a bit further with optimization, so my code is as follows: