Search code examples
ruby-on-railsunit-testingexceptionrescue

How do I disable rescue handlers in Ruby on Rails apps when I'm running functional tests?


I have a number of controllers in my Ruby on Rails apps with a rescue handler at the end of the action that basically catches any unhandled errors and returns some kind of "user friendly" error. However, when I'm doing rake test I'd like to have those default rescue handlers disabled so I can see the full error & stack trace. Is there any automated way to do this?

Update to clarify: I have an action like this:

def foo
  # do some stuff...
rescue
  render :text => "Exception: #{$!}" # this could be any kind of custom render
end

Now when I functional test this, if the exception is raised then I'm going to get just a little bit of info about the exception, but what I'd like is for it to act as though there's no rescue handler there, so I get the full debug information.

Update: SOLUTION

I did this:

  rescue:
    raise unless Rails.env.production?
    render :text => "Exception: #{$!}" # this could be any kind of custom render
  end

Solution

  • Not quite automated, but how modifying your code to re-throw exceptions whenever called within a test?

    Perhaps something like this:

    def foo
      # do some stuff...
    rescue
      raise if ENV["RAILS_ENV"] == "test"
      render :text => "Exception: #{$!}" # this could be any kind of custom render
    end