RSpec + Rails 2.2 = Neglected Rescue Handlers

Upgrading a Rails project to version 2.2 broke controller specs and I couldn't figure out why. The problem i was facing was that the default Rails exception rescuing would not work in the test environment.

I'm seeing alot of something like the following with 'rake spec':

ActiveRecord::RecordNotFound in 'PagesController responding to GET show should not find the record' ActiveRecord::RecordNotFound

An exception is raised during the page request as expected but it prematurely ends the spec instead of going through the rescue handlers I've setup through rescue_from(). I had no idea if this new behavior is expected or a regression. Sure I could do...

lambda{do_get}.should raise_error(ActiveRecord::RecordNotFound)

...but I wasn't looking forward to updating the spec suite to conform. Also, it wouldn't be testing the rescue handler behaviors. Initially, searching via the almighty Google yield no answers. Stepping through code execution using rdebug revealed that RSpec modifies Rails to not rescue exceptions as it normally would. However, the modification also includes a simple way to revert to the default behavior. Score! So now I'm doing:

before(:each) do controller.use_rails_error_handling! end

With that solved, I now know which search terms to use and, lo and behold, Google reveals that somebody had the same problem and already figured it out. Google, why had you forsaken me earlier?

Also, there is more context if you're interested.
Ruby on Rails. December 04, 2008 - 09:01AM. 0 Comments