RailsCasts Pro episodes are now free!

Learn more or hide this

Andrey Pronin's Profile

GitHub User: moonfly

Site: https://www.facebook.com/apronin

Comments by Andrey Pronin

Avatar

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 Postgres
  DatabaseCleaner.clean_with :truncation
  # Normally do transactions-based cleanup
  DatabaseCleaner.strategy = :transaction
end

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 :deletion
  else
    # No JS => run with Rack::Test => transactions are ok
    DatabaseCleaner.start
    spec.run
    DatabaseCleaner.clean
  end
end