Search code examples
ruby-on-railsrubydatabase-cleaner

Rails database_cleaner wipes the entire database


I am trying to set up cleaning of my specs using database_cleaner gem. I have the following simple config in my spec_helper.rb:

require 'database_cleaner'


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

  config.before :suite do
    DatabaseCleaner.strategy = :truncation
  end

  config.before :each do
    DatabaseCleaner.start
  end

  config.after :each do
    DatabaseCleaner.clean
  end
end

Running the spec though, I'm noticing that my database gets completely wiped out after DatabaseCleaner.clean is executed. I'm sure it's meant to clean up only the effects of the test... Any ideas what may cause such behavior?


Solution

  • It's because you've got the strategy set to :truncation, this does indeed clear the whole thing.

    If you want to limit it to just what's done in test, then change the strategy to :transaction

    Broadly speaking, truncation is very simple, and shouldn't be a problem in the TEST database.