Testing entire Rspec suite fails
I have an odd situation whereby if I run an individual rspec model spec file all examples are green, if I test my entire spec/models folder all my examples are green. If I test the controllers they all pass green. If I test the entire suite (via rspec spec) then I get failures - If I remove the controller tests entirely everything is green. Now I'm expecting this is entirely self inflicted but I just can't figure it out.
I've narrowed it down to specific examples in the controller tests - which cause the examples in model specs to fai开发者_运维问答l.
eg. in a notes_controller_spec.rb if this line is present
Note.any_instance.stubs(:valid?).returns(false)
it causes a failure in my models/account_spec.rb
Failure/Error: @account.all_notes.should have(2).notes
ArgumentError:
comparison of Note with Note failed
./app/models/account.rb:293:in `sort'
where line 293 is;
(self.notes + self.transactions.map(&:notes).flatten).sort {|a,b| a.created_at <=> b.created_at }
I'm pretty sure this is going to be one of those face palm moments so be gentle with me!
Are you doing any date setup in a before :all block? These are not transactional and can cause test pollution issues.
Also, I think your syntax might be off here:
Note.any_instance.stubs(:valid?).returns(false)
Should be:
Note.any_instance.stub(:valid?).and_return(false)
I've experienced similar issues with RSpec 3 and Rails 4.1. Whenever I ran the problematic spec file on its own, it would pass, while running the full suite would make it fail.
In my case it was somehow related to time zones. I was explicitly setting the time zone in ApplicationController and for some reason my feature specs didn't like it. If I don't set the time zone when in test environment, everything passes again. e.g.
unless Rails.env.test?
Time.zone = "some timezone value here"
end
I was having a similar problem: Individual model specs passed. When running the whole model suite, I had about 30 failures. What I did was look at the file before all the failures happened. There I found that I was setting things inside threads and using default_scopes like in this railscast.
In a before clause, I printed the Company.current_id
. As I thought, when running individually, Company.current_id
was nil
. When running the suite, Company.current_id
was 2
. This is what happens when you use default scopes. To fix it, I just set Company.current_id
to nil
in the before clause.
Before
describe Service, type: :model do
before do
end
end
After
describe Service, type: :model do
before do
Company.current_id = nil
end
end
精彩评论