ckrf7w
Last Updated: February 25, 2016
·
1.595K
· gacha
Fc44b195ae6025949a40f86bc09a19ad

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
Say Thanks
Respond

1 Response
Add your response

11984
581f2f11f5b021594d86afc9edd04d5a

This saved me a lot of time. Thank you.

over 1 year ago ·