File: anonymous_controller.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 (552 lines) | stat: -rw-r--r-- 15,739 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
Feature: Using an anonymous controller

  Use the `controller` method to define an anonymous controller that will
  inherit from the described class. This is useful for specifying behavior like
  global error handling.

  To specify a different base class you can pass the class explicitly to the
  controller method:

  ```ruby
  controller(BaseController)
  ```

  You can also disable base type inference, in which case anonymous controllers
  will inherit from `ApplicationController` instead of the described class by
  default:

  ```ruby
  RSpec.configure do |c|
    c.infer_base_class_for_anonymous_controllers = false
  end

  RSpec.describe BaseController, type: :controller do
    controller do
      def index; end

      ​# this normally creates an anonymous `BaseController` subclass,
      ​# however since `infer_base_class_for_anonymous_controllers` is
      ​# disabled, it creates a subclass of `ApplicationController`
    end
  end
  ```

  Scenario: Specify error handling in `ApplicationController` with redirect
    Given a file named "spec/controllers/application_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      class ApplicationController < ActionController::Base
        class AccessDenied < StandardError; end

        rescue_from AccessDenied, :with => :access_denied

      private

        def access_denied
          redirect_to "/401.html"
        end
      end

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

        describe "handling AccessDenied exceptions" 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`
    Then the examples should all pass

  Scenario: Specify error handling in `ApplicationController` with render
    Given a file named "spec/controllers/application_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      class ApplicationController < ActionController::Base
        class AccessDenied < StandardError; end

        rescue_from AccessDenied, :with => :access_denied

      private

        def access_denied
          render "errors/401"
        end
      end

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

        describe "handling AccessDenied exceptions" do
          it "renders the errors/401 template" do
            get :index
            expect(response).to render_template("errors/401")
          end
        end
      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Specify error handling in a subclass
    Given a file named "spec/controllers/application_controller_subclass_spec.rb" with:
      """ruby
      require "rails_helper"

      class ApplicationController < ActionController::Base
        class AccessDenied < StandardError; end
      end

      class FoosController < ApplicationController

        rescue_from ApplicationController::AccessDenied,
                    :with => :access_denied

      private

        def access_denied
          redirect_to "/401.html"
        end
      end

      RSpec.describe FoosController, type: :controller do
        controller(FoosController) do
          def index
            raise ApplicationController::AccessDenied
          end
        end

        describe "handling AccessDenied exceptions" 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`
    Then the examples should all pass

  Scenario: Infer base class from the described class
    Given a file named "spec/controllers/base_class_can_be_inferred_spec.rb" with:
      """ruby
      require "rails_helper"

      class ApplicationController < ActionController::Base; end

      class FoosController < ApplicationController; end

      RSpec.describe FoosController, type: :controller do
        controller do
          def index
            render :plain => "Hello World"
          end
        end

        it "creates anonymous controller derived from FoosController" do
          expect(controller).to be_a_kind_of(FoosController)
        end
      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Use `name` and `controller_name` from the described class
    Given a file named "spec/controllers/get_name_and_controller_name_from_described_class_spec.rb" with:
      """ruby
      require "rails_helper"

      class ApplicationController < ActionController::Base; end
      class FoosController < ApplicationController; end

      RSpec.describe "Access controller names", type: :controller do
        controller FoosController do
          def index
            @name = self.class.name
            @controller_name = controller_name
            render :plain => "Hello World"
          end
        end

        before do
          get :index
        end

        it "gets the class name as described" do
          expect(assigns[:name]).to eq('FoosController')
        end

        it "gets the controller_name as described" do
          expect(assigns[:controller_name]).to eq('foos')
        end
      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Invoke `around_filter` and `around_action` in base class
    Given a file named "spec/controllers/application_controller_around_filter_spec.rb" with:
      """ruby
      require "rails_helper"

      class ApplicationController < ActionController::Base
        around_action :an_around_filter

        def an_around_filter
          @callback_invoked = true
          yield
        end
      end

      RSpec.describe ApplicationController, type: :controller do
        controller do
          def index
            render :plain => ""
          end
        end

        it "invokes the callback" do
          get :index

          expect(assigns[:callback_invoked]).to be_truthy
        end
      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Anonymous controllers only create resource routes
    Given a file named "spec/controllers/application_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      if defined?(ActionController::UrlGenerationError)
        ExpectedRoutingError = ActionController::UrlGenerationError
      else
        ExpectedRoutingError = ActionController::RoutingError
      end

      RSpec.describe ApplicationController, type: :controller do
        controller do
          def index
            render :plain => "index called"
          end

          def create
            render :plain => "create called"
          end

          def new
            render :plain => "new called"
          end

          def show
            render :plain => "show called"
          end

          def edit
            render :plain => "edit called"
          end

          def update
            render :plain => "update called"
          end

          def destroy
            render :plain => "destroy called"
          end

          def willerror
            render :plain => "will not render"
          end
        end

        describe "#index" do
          it "responds to GET" do
            get :index
            expect(response.body).to eq "index called"
          end

          it "also responds to POST" do
            post :index
            expect(response.body).to eq "index called"
          end

          it "also responds to PUT" do
            put :index
            expect(response.body).to eq "index called"
          end

          it "also responds to DELETE" do
            delete :index
            expect(response.body).to eq "index called"
          end
        end

        describe "#create" do
          it "responds to POST" do
            post :create
            expect(response.body).to eq "create called"
          end

          # And the rest...
          %w{get post put delete}.each do |calltype|
            it "responds to #{calltype}" do
              send(calltype, :create)
              expect(response.body).to eq "create called"
            end
          end
        end

        describe "#new" do
          it "responds to GET" do
            get :new
            expect(response.body).to eq "new called"
          end

          # And the rest...
          %w{get post put delete}.each do |calltype|
            it "responds to #{calltype}" do
              send(calltype, :new)
              expect(response.body).to eq "new called"
            end
          end
        end

        describe "#edit" do
          it "responds to GET" do
            get :edit, :params => { :id => "anyid" }
            expect(response.body).to eq "edit called"
          end

          it "requires the :id parameter" do
            expect { get :edit }.to raise_error(ExpectedRoutingError)
          end

          # And the rest...
          %w{get post put delete}.each do |calltype|
            it "responds to #{calltype}" do
              send(calltype, :edit, :params => {:id => "anyid"})
              expect(response.body).to eq "edit called"
            end
          end
        end

        describe "#show" do
          it "responds to GET" do
            get :show, :params => { :id => "anyid" }
            expect(response.body).to eq "show called"
          end

          it "requires the :id parameter" do
            expect { get :show }.to raise_error(ExpectedRoutingError)
          end

          # And the rest...
          %w{get post put delete}.each do |calltype|
            it "responds to #{calltype}" do
              send(calltype, :show, :params => {:id => "anyid"})
              expect(response.body).to eq "show called"
            end
          end
        end

        describe "#update" do
          it "responds to PUT" do
            put :update, :params => { :id => "anyid" }
            expect(response.body).to eq "update called"
          end

          it "requires the :id parameter" do
            expect { put :update }.to raise_error(ExpectedRoutingError)
          end

          # And the rest...
          %w{get post put delete}.each do |calltype|
            it "responds to #{calltype}" do
              send(calltype, :update, :params =>  {:id => "anyid"})
              expect(response.body).to eq "update called"
            end
          end
        end

        describe "#destroy" do
          it "responds to DELETE" do
            delete :destroy, :params => { :id => "anyid" }
            expect(response.body).to eq "destroy called"
          end

          it "requires the :id parameter" do
            expect { delete :destroy }.to raise_error(ExpectedRoutingError)
          end

          # And the rest...
          %w{get post put delete}.each do |calltype|
            it "responds to #{calltype}" do
              send(calltype, :destroy, :params => {:id => "anyid"})
              expect(response.body).to eq "destroy called"
            end
          end
        end

        describe "#willerror" do
          it "cannot be called" do
            expect { get :willerror }.to raise_error(ExpectedRoutingError)
          end
        end
      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Draw custom routes for anonymous controllers
    Given a file named "spec/controllers/application_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe ApplicationController, type: :controller do
        controller do
          def custom
            render :plain => "custom called"
          end
        end

        specify "manually draw the route to request a custom action" do
          routes.draw { get "custom" => "anonymous#custom" }

          get :custom
          expect(response.body).to eq "custom called"
        end
      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Draw custom routes for anonymous controllers which don't inherit from application controller
    Given a file named "spec/controllers/other_controller_spec.rb" with:
      """ruby
      require "rails_helper"
      class OtherController < ActionController::Base
      end

      RSpec.describe OtherController, type: :controller do
        controller do
          def custom
            render :plain => "custom called"
          end
        end

        specify "manually draw the route to request a custom action" do
          routes.draw { get "custom" => "other#custom" }

          get :custom
          expect(response.body).to eq "custom called"
        end
      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Draw custom routes for defined controllers
    Given a file named "spec/controllers/application_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      class FoosController < ApplicationController; end

      RSpec.describe ApplicationController, type: :controller do
        controller FoosController do
          def custom
            render :plain => "custom called"
          end
        end

        specify "manually draw the route to request a custom action" do
          routes.draw { get "custom" => "foos#custom" }

          get :custom
          expect(response.body).to eq "custom called"
        end
      end
      """
    When I run `rspec spec`
    Then the examples should all pass

  Scenario: Works with namespaced controllers
    Given a file named "spec/controllers/namespaced_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      class ApplicationController < ActionController::Base; end

      module Outer
        module Inner
          class FoosController < ApplicationController; end
        end
      end

      RSpec.describe Outer::Inner::FoosController, type: :controller do
        controller do
          def index
            @name = self.class.name
            @controller_name = controller_name
            render :plain => "Hello World"
          end
        end

        it "creates anonymous controller derived from the namespace" do
          expect(controller).to be_a_kind_of(Outer::Inner::FoosController)
        end

        it "gets the class name as described" do
          expect{ get :index }.to change{
            assigns[:name]
          }.to eq('Outer::Inner::FoosController')
        end

        it "gets the controller_name as described" do
          expect{ get :index }.to change{
            assigns[:controller_name]
          }.to eq('foos')
        end
      end
      """
    When I run `rspec spec`
    Then the examples should all pass
  Scenario: Refer to application routes in the controller under test
    Given a file named "spec/controllers/application_controller_spec.rb" with:
      """ruby
      require "rails_helper"

      Rails.application.routes.draw do
        match "/login" => "sessions#new", :as => "login", :via => "get"
      end

      RSpec.describe ApplicationController, type: :controller do
        controller do
          def index
            redirect_to login_url
          end
        end

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