Jason
Jason

Reputation: 93

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?

Upvotes: 9

Views: 4627

Answers (4)

Emilia Flo
Emilia Flo

Reputation: 31

The command rspec --bisect, or --order rand:21237 --bisect if you see an error regarding ordering, is a good first step towards debugging. Add --bisect=verbose for more information.

Link to RSpec GH issue

Upvotes: 0

Sean Szurko
Sean Szurko

Reputation: 253

I Had a similar issue today that took quite a bit of time to resolve.

This fails:

module SomeModule
  describe SomeController do
    it 'does something' 
  end
end

This passes:

describe SomeModule::SomeController do
  it 'does something'
end

The issue has to deal with scoping.

Upvotes: 0

Ben
Ben

Reputation: 1342

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!

Upvotes: 1

Mirko
Mirko

Reputation: 5286

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

Upvotes: 6

Related Questions