Search code examples
ruby-on-railscucumbercapybara

Database cleaner wipes out all tables even when using :except option


I'm having an issue with database cleaner on a rails project. I use a sqlite3 database in my test environment, and it has a number of tables containing reference data, populated by the db:test:prepare task, that does not need to be wiped between tests.

I have a number of cucumber scenarios tagged with @javascript using the webdriver driver, and some without that tag.

In my env.rb file, I have configured database cleaner to use the truncation strategy, with the except option:

DatabaseCleaner.strategy = :truncation, {:except => %w[ignore me]}

DatabaseCleaner.clean is called after each scenario, and works as expected on the javascript-tagged scenarios.

However, for the non javascript scenarios it is truncating the entire database, including the tables listed in the :except array. I've also tried calling DatabaseCleaner.clean_with, which didn't work either.


Solution

  • I was running into a similar error, except I was seeing the problem when running my @javascript scenarios.

    After much googling, reading and hair-pulling I came across this post

    I looked in the database_cleaner hook file mentioned there (cucumber-rails-0.4.0/lib/cucumber/rails/hooks/database_cleaner.rb) and lo-and-behold it sets the database_cleaner strategy to :truncation without exceptions before each scenario with the following tags: @no-txn,@selenium,@culerity,@celerity,@javascript.

    Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
      DatabaseCleaner.strategy = :truncation
    end
    

    I copied and pasted the before statement into my cucumber env.rb file and added my :except statement to it. That seems to have fixed the issue.

    Are you certain it's not your @javascript scenarios that are causing the problem?