What rescue hooks


#1

A few of our tests attempt to test controllers with production-style
rescuing. Before the current rspec/Rails 2.2.2 combo, it was fairly
easy to
set up for these tests:

it “should throw a 404 for strange actions” do
controller.use_rails_error_handling!
get :strange_action_name
response.should be_missing
response.body.should include(“You’ve hit our custom 404 page.”)
end

As of trunk rspec/Rails 2.2.2, this doesn’t work. It turns out that
ActionController::TestCase has invented its own hook for testing.
Without
this hook set, you end up
in ‘local development’ mode. So to get into production mode, you need
to do
this:

it “should throw a 404 for strange actions” do
controller.use_rails_error_handling! # rspec no longer throws the
error
without hitting Rails logic
rescue_action_in_public! # ActionController::TestCase no longer
considers
this request a local request

get :strange_action_name
response.should be_missing
response.body.should include(“You’ve hit our custom 404 page.”)
end

Of course, if you have ‘rescue_from’ handlers, these will happen no
matter
what.

I guess my question is, now that the ‘rescue_action_in_public!’ hook
exists
in Rails, does it still make sense for rspec-rails to have its own
method?
Might it make sense
to merge the two functionalities in some way?

=Nathan


#2

On Fri, Feb 13, 2009 at 5:20 PM, Nathan W. removed_email_address@domain.invalid
wrote:

this request a local request
in Rails, does it still make sense for rspec-rails to have its own method?
Might it make sense
to merge the two functionalities in some way?

Nathan,

Thanks for bringing this all up. I’ve made some changes and additions
based on this and a few other conversations. Here’s how it works now
(latest in git and latest github gem, and the next formal release):

  1. controller.use_rails_error_handling! now works as it did in 1.1.12
  2. controller.use_rails_error_handling! is now deprecated, because
    rescue_action_in_public! does exactly the same thing without
    rspec-rails having to monkey patch rails
  3. added bypass_rescue, which you can use to bypass any rescued errors
    (defined in rescue_from)

This allows you to do any of the following:

  1. specify un_rescued errors with “should raise_error” (default)
  2. specify un_rescued errors with status codes (using
    rescue_action_in_public!)
  3. specify rescued errors with bypass_rescue with “should raise_error”
  4. specify rescued errors with bypass_rescue with status codes (using
    rescue_action_in_public!)

Both rescue_action_in_public! and bypass_rescue can be declared
per-example, or in before(:each).

Questions?