Search code examples
ruby-on-railspostgresqlrspecdatabase-cleaner

DB Cleaner not cleaning database after each test after switching from sqlite to PostgreSQL


This config worked fine when I was using sqlite:

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    controller.stub(:should_navigate_user?).and_return(false) rescue ""
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end

When I switched to PostgreSQL I began getting errors when tests depended on the id number of entries in the database. I looked at the database during the tests and it was never cleaning the database.

If I switch to the following config:

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    DatabaseCleaner.clean
    controller.stub(:should_navigate_user?).and_return(false) rescue ""
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end

All of my tests pass, but I'm having to call clean before and after my methods tests?

I shouldn't have to clean the database before and after each test - right? I feel like I'm misunderstanding something somewhere. What am I failing to understand.


Solution

  • You used sqlite in non-persistent mode. PostgreSQL doesn't have such options, always persistent. Use drop database/create database to clean it complettely.