Skip to content
This repository
Browse code

Re-introduced :bypass_rescue in ControllerExampleGroup

- Closes #426
  • Loading branch information...
commit e373d795da904bbd92fdc7102ad7114fdfbe70ab 1 parent 24a3117
Lenny Marks lenny authored dchelimsky committed
75 features/controller_specs/error_handling.feature
... ... @@ -0,0 +1,75 @@
  1 +Feature: Default Rails error handling can be overridden using :bypass_rescue
  2 +
  3 + Errors from a controller action will normally be handled by the
  4 + default Rails exception handling mechanism (i.e. they will not
  5 + propagate out). If handling of particular exceptions
  6 + is implemented outside of the controller under test(e.g. an inherited :rescue_from),
  7 + it may be preferred to assert only that the expected exception was raised.
  8 + This can be accomplished using :bypass_rescue
  9 +
  10 + Background:
  11 + Given a file named "spec/controllers/gadgets_controller_spec_context.rb" with:
  12 + """
  13 + class ErrorHandlingAccessDenied < StandardError; end
  14 +
  15 + class ApplicationController < ActionController::Base
  16 + rescue_from ErrorHandlingAccessDenied, :with => :access_denied
  17 +
  18 + private
  19 +
  20 + def access_denied
  21 + redirect_to "/401.html"
  22 + end
  23 + end
  24 + """
  25 +
  26 + Scenario: Standard Rails exception handling (the default)
  27 + Given a file named "spec/controllers/gadgets_controller_spec.rb" with:
  28 + """
  29 + require "spec_helper"
  30 +
  31 + require 'controllers/gadgets_controller_spec_context'
  32 +
  33 + describe GadgetsController do
  34 + before do
  35 + def controller.index
  36 + raise ErrorHandlingAccessDenied
  37 + end
  38 + end
  39 +
  40 + describe "index" do
  41 + it "redirects to the /401.html page" do
  42 + get :index
  43 + response.should redirect_to("/401.html")
  44 + end
  45 + end
  46 + end
  47 + """
  48 + When I run `rspec spec/controllers/gadgets_controller_spec.rb`
  49 + Then the examples should all pass
  50 +
  51 + Scenario: Rails exception rescuing can be bypassed with :bypass_rescue
  52 + Given a file named "spec/controllers/gadgets_controller_spec.rb" with:
  53 + """
  54 + require "spec_helper"
  55 +
  56 + require 'controllers/gadgets_controller_spec_context'
  57 +
  58 + describe GadgetsController do
  59 + before do
  60 + def controller.index
  61 + raise ErrorHandlingAccessDenied
  62 + end
  63 + end
  64 +
  65 + describe "index" do
  66 + it "raises AccessDenied" do
  67 + bypass_rescue
  68 + expect { get :index }.to raise_error(ErrorHandlingAccessDenied)
  69 + end
  70 + end
  71 + end
  72 + """
  73 + When I run `rspec spec/controllers/gadgets_controller_spec.rb`
  74 + Then the examples should all pass
  75 +
6 lib/rspec/rails/example/controller_example_group.rb
@@ -144,6 +144,12 @@ def name; "AnonymousController" end
144 144 module InstanceMethods
145 145 attr_reader :controller, :routes
146 146
  147 + def bypass_rescue
  148 + def controller.rescue_with_handler(exception)
  149 + raise exception
  150 + end
  151 + end
  152 +
147 153 def method_missing(method, *args, &block)
148 154 if @orig_routes && @orig_routes.named_routes.helpers.include?(method)
149 155 controller.send(method, *args, &block)

0 comments on commit e373d79

Please sign in to comment.
Something went wrong with that request. Please try again.