Last Updated: February 25, 2016
·
1.968K
· gacha

Ultimate rspec/capybara database cleaner

This should be saved somewhere in spec/support/database_cleaner.rb. Two key features are: example.metadata[:type] to determinate that it's an feature and should be run with truncation and second is to wrap it all into around hook insted of before, to execute after other after hook's - this helped me with some wierd DB dedlocks.

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.around(:each) do |example|
    DatabaseCleaner.strategy = if example.metadata[:js] || example.metadata[:type] == 'feature'
      :truncation
    else
      :transaction
    end
    DatabaseCleaner.start

    example.run

    Capybara.reset_sessions!
    DatabaseCleaner.clean
  end
end

1 Response
Add your response

This saved me a lot of time. Thank you.

over 1 year ago ·