Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Re-introduced :bypass_rescue in ControllerExampleGroup

  • Loading branch information...
commit e0cb0fc148c7fc2bb22501f9d714aca3c055b1f9 1 parent 24a3117
@lenny authored
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)
Please sign in to comment.
Something went wrong with that request. Please try again.