You may have a point when you’re writing specs that are meant to anticipate a 404 Not Found from a resource. You have a rescue like this:


rescue ActiveResource::ResourceNotFound
return nil

..and now you need a test for it. In Rspec it’s not obvious, but easy. First, you need to stub your actions in your spec:


That sounds good, but it won’t work because it will fail with:

ArgumentError: wrong number of arguments (0 for 1)

Arguments? I need to pass arguments to an error? I guess I do. Maybe the status code?

But no:

NoMethodError: undefined method `code’ for 404:Fixnum

Ah ha! It needs some kind of object passed in to new()! But, what is it? We have no way of knowing off hand what object should be there. Wait, though, we’re INSIDE an environment that exists for making throw-away objects!

Something.stub!(:some_method).and_raise('err', :code => '404'))

Try that, you’ll get your exception!

  • Nico

    Thank you very much it very helpful ;)

  • James Moxley

    Just wanted to say thanks for posting the solution, hit the problem this morning, it was a real hair puller.

  • Mike Levine

    You rock! Very helpful. One small change: I had to use `double` instead of `mock.` No biggie at all of course. Just wanted to mention in case this helps someone else.