Testing the entire rspec package fails

I have a strange situation when I run a separate rspec model specification file, all the examples are green, if I test my entire spec / models folder, all my examples are green. If I test the controllers, they all go green. If I test the entire package (via the rspec specifier), I get crashes. If I remove the controller tests, everything will be green. Now I expect this to be completely imposed, but I just can't understand.

I narrowed it down to concrete examples in controller tests, which leads to failure of the examples in the model specifications.

eg. in the notes_controller_spec.rb file, if this line is present

Note.any_instance.stubs(:valid?).returns(false) 

it crashes 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 is line 293:

  (self.notes + self.transactions.map(&:notes).flatten).sort {|a,b| a.created_at <=> b.created_at } 

I am sure this will be one of those moments in the palm of your hand, so be careful with me!

+6
source share
3 answers

Is any date setting done earlier: the whole block? They are not transactional and may cause trial contamination problems.

Also, I think your syntax can be turned off here:

  Note.any_instance.stubs(:valid?).returns(false) 

Must be:

  Note.any_instance.stub(:valid?).and_return(false) 
+1
source

I am having similar issues with RSpec 3 and Rails 4.1. Whenever I ran the problematic spec file on its own, it would go through, when it started the full set it would work.

In my case, it was somehow related to time zones. I explicitly set the time zone in the ApplicationController, and for some reason, the specifications of my function did not like. If I do not set the time zone in the test environment, everything passes again. eg.

 unless Rails.env.test? Time.zone = "some timezone value here" end 
0
source

I had a similar problem: individual model specifications passed. When I started the entire set of models, I had about 30 failures. What I did was look at the file before all the crashes happen. There I discovered that I was setting things up inside threads and using default_scopes, as in this railscast .

In the sentence above, I typed Company.current_id . As I thought, when starting individually, Company.current_id was nil . When starting the Company.current_id package was 2 . This is what happens when using the default areas. To fix this, 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 
0
source

Source: https://habr.com/ru/post/893189/


All Articles