2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

routingでtypoしてるとrspecでActionController::UrlGenerationErrorがでる件

Posted at

概要

routingでtypoしていると、railsは無視して解釈してアクセスできるようにしてくれますが、rspecはそのコントローラーのテストでエラーを返します。

例えば

config/routes.rb
resources :examples, excpet: %i[new update destroy] 

exceptexcpetのように書く。

こんな感じで解決される。

ちなみにrails routesはこんな感じになる

examples      GET         /examples(.:format)            examples#index {:excpet=>[:new, :update, :destroy]}
              POST        /examples(.:format)            examples#create {:excpet=>[:new, :update, :destroy]}
new_example   GET         /examples/new(.:format)        examples#new {:excpet=>[:new, :update, :destroy]}
edit_example  GET         /examples/:id/edit(.:format)   examples#edit {:excpet=>[:new, :update, :destroy]}
example       GET         /examples/:id(.:format)        examples#show {:excpet=>[:new, :update, :destroy]}
              PATCH       /examples/:id(.:format)        examples#update {:excpet=>[:new, :update, :destroy]}
              PUT         /examples/:id(.:format)        examples#update {:excpet=>[:new, :update, :destroy]}
              DELETE      /examples/:id(.:format)        examples#destroy {:excpet=>[:new, :update, :destroy]}

routingではtypoは解釈されず、resourcesは全て解釈される。

そしてcontrollerを作りアクセスすると

examples_controller.rb
class ExamplesController < ApplicationController

  def index
  end
{"method":"GET","path":"/examples","format":"html","controller":"ExamplesController","action":"i
ndex","status":200,"duration":4429.18,"view":4345.9,"db":3.32,"backtrace":null,"host":"localhost
","message":"[200] GET /terms_of_uses (TermsOfUsesController#index)"}

そして普通にアクセスできてしまう。

しかしこの状態でrspecのcontroller_specを作成して実行すると

require "rails_helper"

RSpec.describe ExamplesController, type: :controller do

  describe "GET #index" do
    it "returns a 200 status code" do
      get :index
      expect(response.status).to eq(200)
    end
  end
end
spec/controller/exmaples_spec.rb
  1) AnnouncementsController GET #index responds successfully with an HTTP 200 status code
     Failure/Error: get :index
     
     ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"announcements"}
     # /usr/local/bundle/gems/devise-4.7.1/lib/devise/test/controller_helpers.rb:35:in `block in process'
     # /usr/local/bundle/gems/devise-4.7.1/lib/devise/test/controller_helpers.rb:102:in `catch'
     # /usr/local/bundle/gems/devise-4.7.1/lib/devise/test/controller_helpers.rb:102:in `_catch_warden'
     # /usr/local/bundle/gems/devise-4.7.1/lib/devise/test/controller_helpers.rb:35:in `process'
     # ./spec/controllers/youtube_channel_platform_histories_controller_spec.rb:58:in `block (3 levels) in <top (required)>'

このように解釈してくれない。

結論

typoするやつ許すまじ(ブーメラン)

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?