File: bypass_rescue.feature

package info (click to toggle)
ruby-rspec-rails 7.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,796 kB
  • sloc: ruby: 11,068; sh: 198; makefile: 6
file content (74 lines) | stat: -rw-r--r-- 2,131 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Feature: Using `bypass_rescue`

  Use `bypass_rescue` to bypass both Rails' default handling of errors in
  controller actions, and any custom handling declared with a `rescue_from`
  statement.

  This lets you specify details of the exception being raised, regardless of
  how it might be handled upstream.

  Background:
    Given a file named "spec/controllers/gadgets_controller_spec_context.rb" with:
      """ruby
      class AccessDenied < StandardError; end

      class ApplicationController < ActionController::Base
        rescue_from AccessDenied, :with => :access_denied

        private

        def access_denied
          redirect_to "/401.html"
        end
      end
      """

  Scenario: Standard exception handling using `rescue_from`
    Given a file named "spec/controllers/gadgets_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      require 'controllers/gadgets_controller_spec_context'

      RSpec.describe GadgetsController, type: :controller do
        before do
          def controller.index
            raise AccessDenied
          end
        end

        describe "index" do
          it "redirects to the /401.html page" do
            get :index
            expect(response).to redirect_to("/401.html")
          end
        end
      end
      """
    When I run `rspec spec/controllers/gadgets_controller_spec.rb`
    Then the examples should all pass

  Scenario: Bypass `rescue_from` handling with `bypass_rescue`
    Given a file named "spec/controllers/gadgets_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      require 'controllers/gadgets_controller_spec_context'

      RSpec.describe GadgetsController, type: :controller do
        before do
          def controller.index
            raise AccessDenied
          end
        end

        describe "index" do
          it "raises AccessDenied" do
            bypass_rescue
            expect { get :index }.to raise_error(AccessDenied)
          end
        end
      end
      """
    When I run `rspec spec/controllers/gadgets_controller_spec.rb`
    Then the examples should all pass