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

1

6

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
Jack answered 14/2, 2013 at 17:51 Comment(2)
I didn't need the accepted answer but your question was very useful for me! I am using the 'if JS use truncation' block and it's working perfectly! Thanks!Holstein
Glad to help. The number of hours I spent messing around with this was not fun.Jack
A
3

this should do it, let me know

config.after(:all, :type => :feature) do
  DatabaseCleaner.clean_with :truncation
end
Ardent answered 14/2, 2013 at 18:26 Comment(2)
wouldn't you want to clean the database after auch test run?Wart
I tried with config.before(:suite, :type => :feature), but I had no luck, the DatabaseCleaner.strategy = :truncation is always running :/Anyway

© 2022 - 2024 — McMap. All rights reserved.