Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

individual spec passes when run alone, but fails when all specs are run

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?

like image 558
Jason Avatar asked Sep 03 '10 15:09

Jason


2 Answers

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...

like image 199
Mirko Avatar answered Sep 28 '22 04:09

Mirko


Just in case it helps others: I had a similar problem and found out I had

  • stray Rspec.configures that were overriding for all tests down the line
  • 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!

like image 44
Ben Avatar answered Sep 28 '22 06:09

Ben