[rspec-users] RSpec2 error codes testing

Andy Lindeman andy at andylindeman.com
Sun Aug 4 13:03:41 UTC 2013

On 07/20/2013 08:20 AM, Robert Lis wrote:
> I have an issue with testing error codes. If a record is not found, it
> raises exception ActiveRecord::RecordNotFound. The thing is, I wanna
> check if 404 is returned. If I go ahead and just check :
>  response.status.should == 404
> Then this will make the test case fail as RecordNotFound exception is
> thrown and not caught. If I go ahead and say
> expect {<call controller action>}.to
> raise_error(ActiveRecord::RecordNotFound)
> and then check for the status code, then it will fail again, as this
> catches the exception and the response code is 200.
> How should I test if 404 is returned in this kind of case ?

You'll need to go up a level and use a request or feature spec.
Controller specs don't run through the full stack, so you get the lower
level exception ... not a 404.

It's a bit confusing, I agree. Controllers are a weird thing in the
first place :/

More information about the rspec-users mailing list