File: have_http_status_matcher.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 (151 lines) | stat: -rw-r--r-- 4,229 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Feature: `have_http_status` matcher

  The `have_http_status` matcher is used to specify that a response returns a
  desired status code. It accepts one argument in any of the following formats:

  * numeric code
  * status name as defined in `Rack::Utils::SYMBOL_TO_STATUS_CODE`
  * generic status type (`:success`, `:missing`, `:redirect`, or `:error`)

  The matcher works on any `response` object. It is available for use in
  [controller specs](../controller-specs), [request specs](../request-specs), and [feature specs](../feature-specs).

  Scenario: Checking a numeric status code
    Given a file named "spec/controllers/application_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe ApplicationController, type: :controller do

        controller do
          def index
            render :json => {}, :status => 209
          end
        end

        describe "GET #index" do
          it "returns a 209 custom status code" do
            get :index
            expect(response).to have_http_status(209)
          end
        end

      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Checking a symbolic status name
    Given a file named "spec/controllers/application_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe ApplicationController, type: :controller do

        controller do
          def index
            render :json => {}, :status => :see_other
          end
        end

        describe "GET #index" do
          it "returns a :see_other status code" do
            get :index
            expect(response).to have_http_status(:see_other)
          end
        end

      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Checking a symbolic generic status type
    Given a file named "spec/controllers/application_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe ApplicationController, type: :controller do

        controller do
          def index
            render :json => {}, :status => :bad_gateway
          end
        end

        describe "GET #index" do
          it "returns a some type of error status code" do
            get :index
            expect(response).to have_http_status(:error)
          end
        end

      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Using in a controller spec
    Given a file named "spec/controllers/gadgets_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe GadgetsController, type: :controller do

        describe "GET #index" do
          it "returns a 200 OK status" do
            get :index
            expect(response).to have_http_status(:ok)
          end
        end

      end
      """
    When I run `rspec spec/controllers/gadgets_spec.rb`
    Then the examples should all pass

  Scenario: Using in a request spec
    Given a file named "spec/requests/gadgets/widget_management_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "Widget management", type: :request do

        it "creates a Widget and redirects to the Widget's page" do
          get "/widgets/new"
          expect(response).to have_http_status(:ok)

          post "/widgets", :params => { :widget => {:name => "My Widget"} }
          expect(response).to have_http_status(302)

          follow_redirect!

          expect(response).to have_http_status(:success)
        end

      end
      """
    When I run `rspec spec/requests`
    Then the examples should all pass

  @capybara
  Scenario: Using in a feature spec
    Given a file named "spec/features/widget_management_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.feature "Widget management", type: :feature do

        scenario "User creates a new widget" do
          visit "/widgets/new"
          expect(page).to have_http_status(200)

          click_button "Create Widget"

          expect(page).to have_http_status(:success)
        end

      end
      """
    When I run `rspec spec/features/widget_management_spec.rb`
    Then the example should pass