Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adaRn/f9d7d3d6cd310e1e3dd6fb804156eb60 to your computer and use it in GitHub Desktop.
Save adaRn/f9d7d3d6cd310e1e3dd6fb804156eb60 to your computer and use it in GitHub Desktop.
DatabaseCleaner cleaning database too early (Capybara, PhantomJS, poltergeist)

Solution:

In your helper change

config.after(:each) do
  DatabaseCleaner.clean
end

to

config.append_after(:each) do
  DatabaseCleaner.clean
end

Why this works?

after is in fact an alias for prepend_after, which means DatabaseCleaner.clean will run before Capybara and its drivers terminate all connections.

Stopping Capybara first and then cleaning the database will ensure no errors will arise from requests that Capybara's driver makes because of already cleaned database. The requests that are yielding errors don't even necessarily have to be explicitly issued by you - it's enough that one of tons of requests that a browser makes fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment