Search code examples
ruby-on-railstestingcapybaradatabase-cleaner

Rails Rspec Capybara and DatabaseCleaner - using truncation only on feature tests


I saw this cool method for only using Database cleaners :truncation for capybara test using :js => true

In spec_helper.rb:

config.before(:each) do
  DatabaseCleaner.strategy = if example.metadata[:js]
    :truncation
  else
    :transaction
  end
  DatabaseCleaner.start
end

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

The problem is that any feature test done with capybara seems to need the cleaning strategy to be :truncation.

All the other specs, however, are fine with :transaction, which is significantly faster.

Is there a way of specifying strategy for only capybara feature tests? SOmething like:

DataCleaner.strategy( :truncation ) if :type => :feature

Solution

  • this should do it, let me know

    config.after(:all, :type => :feature) do
      DatabaseCleaner.clean_with :truncation
    end