I have 30 specs in my foo_controller_spec.rb and when I run the entire file using spec, I get 4 failures and 2 pending. When I run the 4 failing specs individually, 3 of them still fail, but one of them passes. At first I thought it was a database issue, that the data wasn't being cleaned out properly between runs. So I installed database_cleaner (http://github.com/bmabey/database_cleaner) and added this code to my spec_helper:
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
Sham.reset
login
end
config.after(:each) do
DatabaseCleaner.clean
end
Now when I run my specs I can see that the tables are truncated between each spec so I know it's working, but the spec still fails when run with all the other specs but passes when run alone. What am I missing here?
I had exactly the same issue and was going out of my mind!
I tracked the issue like this (with Textmate): select your files in project drawer (multiple files) and then go to "Bundles > Rspec > Run examples in selected files/directories".
The trick is to find which file is causing interference with others.
For me it was changing of I18n.locale in one file which caused the locale to be changed for all other examples!
I lost a few hours going nuts with this...
Just in case it helps others: I had a similar problem and found out I had
WebMocks enabled in one test that seem to cascade to following on tests until i I included in the spec_helper so connections were on by default
RSpec.configure do |config| config.before(:suite) do WebMock.allow_net_connect!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With