Forum: RSpec What rescue hooks

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6f146aa044b16ff5850d099e0166caed?d=identicon&s=25 Nathan Wilmes (Guest)
on 2009-02-14 00:55
(Received via mailing list)
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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-18 00:12
(Received via mailing list)
On Fri, Feb 13, 2009 at 5:20 PM, Nathan Wilmes <nathan@pivotallabs.com>
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?
This topic is locked and can not be replied to.