Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Re-introduce :bypass_rescue in ControllerExampleGroup #426

Closed
wants to merge 1 commit into from

2 participants

@lenny

Here's a potential patch for:

#425

The error_handling.feature might not be the right direction but I figured I'd give it a shot. It worked for me with Ruby 1.8.7 and Rails 3.0.9

Thanks,
-lenny

@dchelimsky dchelimsky was assigned
@dchelimsky dchelimsky referenced this pull request from a commit
@dchelimsky dchelimsky add spec for bypass_rescue e028626
@dchelimsky dchelimsky referenced this pull request from a commit
@dchelimsky dchelimsky Changelog b306f55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 15, 2011
  1. @lenny
This page is out of date. Refresh to see the latest.
View
75 features/controller_specs/error_handling.feature
@@ -0,0 +1,75 @@
+Feature: Default Rails error handling can be overridden using :bypass_rescue
+
+ Errors from a controller action will normally be handled by the
+ default Rails exception handling mechanism (i.e. they will not
+ propagate out). If handling of particular exceptions
+ is implemented outside of the controller under test(e.g. an inherited :rescue_from),
+ it may be preferred to assert only that the expected exception was raised.
+ This can be accomplished using :bypass_rescue
+
+ Background:
+ Given a file named "spec/controllers/gadgets_controller_spec_context.rb" with:
+ """
+ class ErrorHandlingAccessDenied < StandardError; end
+
+ class ApplicationController < ActionController::Base
+ rescue_from ErrorHandlingAccessDenied, :with => :access_denied
+
+ private
+
+ def access_denied
+ redirect_to "/401.html"
+ end
+ end
+ """
+
+ Scenario: Standard Rails exception handling (the default)
+ Given a file named "spec/controllers/gadgets_controller_spec.rb" with:
+ """
+ require "spec_helper"
+
+ require 'controllers/gadgets_controller_spec_context'
+
+ describe GadgetsController do
+ before do
+ def controller.index
+ raise ErrorHandlingAccessDenied
+ end
+ end
+
+ describe "index" do
+ it "redirects to the /401.html page" do
+ get :index
+ response.should redirect_to("/401.html")
+ end
+ end
+ end
+ """
+ When I run `rspec spec/controllers/gadgets_controller_spec.rb`
+ Then the examples should all pass
+
+ Scenario: Rails exception rescuing can be bypassed with :bypass_rescue
+ Given a file named "spec/controllers/gadgets_controller_spec.rb" with:
+ """
+ require "spec_helper"
+
+ require 'controllers/gadgets_controller_spec_context'
+
+ describe GadgetsController do
+ before do
+ def controller.index
+ raise ErrorHandlingAccessDenied
+ end
+ end
+
+ describe "index" do
+ it "raises AccessDenied" do
+ bypass_rescue
+ expect { get :index }.to raise_error(ErrorHandlingAccessDenied)
+ end
+ end
+ end
+ """
+ When I run `rspec spec/controllers/gadgets_controller_spec.rb`
+ Then the examples should all pass
+
View
6 lib/rspec/rails/example/controller_example_group.rb
@@ -144,6 +144,12 @@ def name; "AnonymousController" end
module InstanceMethods
attr_reader :controller, :routes
+ def bypass_rescue
+ def controller.rescue_with_handler(exception)
+ raise exception
+ end
+ end
+
def method_missing(method, *args, &block)
if @orig_routes && @orig_routes.named_routes.helpers.include?(method)
controller.send(method, *args, &block)
Something went wrong with that request. Please try again.