With the help of an excellent post by Avdi Grimm, I installed database_cleaner and configured it to work in the Capybara/poltergeist environment. However, this led to issues where tests didn’t seem to be properly cleaning up after themselves.
Thanks to this reply I tracked down the problem to a bug in RSpec 2.13. If you are going to use database_cleaner with Capybara/poltergeist, make sure you are using RSpec >= 2.14.0.rc1. For my Rails app, this is what I put in my
gem 'rspec-rails', "~> 2.14.0.rc1"
At this point, I thought I had the problem solved.
And yet, there was more nondeterminism to come
Tests were still failing with ActiveRecord
not found exceptions. Less frequently, but still enough to ruin the utility of my test suite. After much trial and error, I added a single line to the database_cleaner config in the reply above, and it appears to actually work. Here’s my
RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.clean_with(:truncation) end config.before(:each) do DatabaseCleaner.strategy = Capybara.current_driver == :rack_test ? :transaction : :truncation DatabaseCleaner.clean DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end end