File: request_spec.feature

package info (click to toggle)
ruby-rspec-rails 8.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,804 kB
  • sloc: ruby: 10,881; sh: 198; makefile: 6
file content (158 lines) | stat: -rw-r--r-- 5,079 bytes parent folder | download | duplicates (2)
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
152
153
154
155
156
157
158
Feature: Request specs

  Request specs provide a thin wrapper around Rails' integration tests, and are
  designed to drive behavior through the full stack, including routing
  (provided by Rails) and without stubbing (that's up to you).

  Request specs are marked by `type: :request` or if you have set
  `config.infer_spec_type_from_file_location!` by placing them in `spec/requests`.

  With request specs, you can:

  * specify a single request
  * specify multiple requests across multiple controllers
  * specify multiple requests across multiple sessions

  Check the rails documentation on integration tests for more information.

  RSpec provides two matchers that delegate to Rails assertions:

  ```ruby
  render_template # delegates to assert_template
  redirect_to     # delegates to assert_redirected_to
  ```

  Check the Rails docs for details on these methods as well.

  [Capybara](https://github.com/teamcapybara/capybara) is not supported in
  request specs. The recommended way to use Capybara is with
  [feature specs](../feature-specs/feature-spec).

  Scenario: Specify managing a Widget with Rails integration methods
    Given a file named "spec/requests/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 render_template(:new)

          post "/widgets", :params => { :widget => {:name => "My Widget"} }

          expect(response).to redirect_to(assigns(:widget))
          follow_redirect!

          expect(response).to render_template(:show)
          expect(response.body).to include("Widget was successfully created.")
        end

        it "does not render a different template" do
          get "/widgets/new"
          expect(response).to_not render_template(:show)
        end
      end
      """
    When I run `rspec spec/requests/widget_management_spec.rb`
    Then the example should pass

  Scenario: Requesting a JSON response
    Given a file named "spec/requests/widget_management_spec.rb" with:
    """ruby
    require "rails_helper"

    RSpec.describe "Widget management", type: :request do
      it "creates a Widget" do
        headers = { "ACCEPT" => "application/json" }
        post "/widgets", :params => { :widget => {:name => "My Widget"} }, :headers => headers

        expect(response.content_type).to eq("application/json; charset=utf-8")
        expect(response).to have_http_status(:created)
      end
    end
    """
    When I run `rspec spec/requests/widget_management_spec.rb`
    Then the example should pass

  Scenario: Providing JSON data
    Given a file named "spec/requests/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
        headers = { "CONTENT_TYPE" => "application/json" }
        post "/widgets", :params => '{ "widget": { "name":"My Widget" } }', :headers => headers
        expect(response).to redirect_to(assigns(:widget))
      end

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

  Scenario: Using engine route helpers
    Given a file named "spec/requests/widgets_spec.rb" with:
      """ruby
      require "rails_helper"

      # A very simple Rails engine
      module MyEngine
        class Engine < ::Rails::Engine
          isolate_namespace MyEngine
        end

        class LinksController < ::ActionController::Base
          def index
            render plain: 'hit_engine_route'
          end
        end
      end

      MyEngine::Engine.routes.draw do
        resources :links, :only => [:index]
      end

      Rails.application.routes.draw do
        mount MyEngine::Engine => "/my_engine"
      end

      module MyEngine
        RSpec.describe "Links", type: :request do
          include Engine.routes.url_helpers

          it "redirects to a random widget" do
            get links_url
            expect(response.body).to eq('hit_engine_route')
          end
        end
      end
      """
    When I run `rspec spec`
    Then the example should pass

  Scenario: Testing subdomain constrained requests
    Given a file named "spec/requests/widgets_spec.rb" with:
      """ruby
      require "rails_helper"

      Rails.application.routes.draw do
        resources :widgets, constraints: { subdomain: "api" }
      end

      RSpec.describe "Widget management", type: :request do
        before { host! "api.example.com" }

        it "creates a Widget" do
          headers = { "ACCEPT" => "application/json" }
          post "/widgets", :params => { :widget => { :name => "My Widget" } }, :headers => headers

          expect(response.content_type).to start_with("application/json")
          expect(response).to have_http_status(:created)
        end
      end
      """
    When I run `rspec spec`
    Then the example should pass