RSpec2 error checking

I have a problem with testing error codes. If the record is not found, it throws an ActiveRecord :: RecordNotFound exception. The thing is, I want to check if 404 is returned. If I continue and just check:

response.status.should == 404 

Then this will cause the test case to fail, as the RecordNotFound exception will be thrown and not caught. If I continue and say

 expect {<call controller action>}.to raise_error(ActiveRecord::RecordNotFound) 

and then check the status code, after which it will fail again, as this will lead to an exception, and the response code is 200.

How to check if 404 returns in this case?

+4
source share
2 answers

If you do not want to check exceptions in other examples, you can set the following parameters in config/environments/test.rb :

 config.consider_all_requests_local = false config.action_dispatch.show_exceptions = true 
0
source

I'm not sure I understand the specifics well to know if this will help you, but check bypass_rescue as described in https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/bypass-rescue .

0
source

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


All Articles