Why am I getting an "ActionController::UrlGenerationError: No route matches..." error?

Why am I getting an "ActionController::UrlGenerationError: No route matches..." error?

我正在使用 ruby 2.3.1 和 rails 5.0.7。当 运行 RSpec:

时出现以下错误

失败:

1) AuthenticationController POST /auth/login 当请求有效时 returns 身份验证令牌 Failure/Error: 之前 { post 'auth/login', params: valid_credentials , headers: headers }

 ActionController::UrlGenerationError:
   No route matches {:action=>"auth/login", :controller=>"authentication", :email=>"foo@bar.com", :password=>"foobar"}
 # ./spec/controllers/authentication_controller_spec.rb:32:in `block (4 levels) in <top (required)>'
 # ./spec/rails_helper.rb:85:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:84:in `block (2 levels) in <top (required)>'

2) AuthenticationController POST /auth/login 当请求无效时 returns 失败消息 Failure/Error: 之前 { post '/auth/login', params: invalid_credentials, headers: headers }

 ActionController::UrlGenerationError:
   No route matches {:action=>"/auth/login", :controller=>"authentication", :email=>"paolo.jakubowski@quitzonbartoletti.net", :password=>"30NoVoGu7h"}
 # ./spec/controllers/authentication_controller_spec.rb:41:in `block (4 levels) in <top (required)>'
 # ./spec/rails_helper.rb:85:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:84:in `block (2 levels) in <top (required)>'

这是我的 routes.rb:

Rails.application.routes.draw做 # 有关此文件中可用 DSL 的详细信息,请参阅 http://guides.rubyonrails.org/routing.html

资源:待办事项

 resources :items

结束

post'auth/login',至:'authentication#authenticate'

post'signup',至:'users#create'

结束

生成的路线如下:

前缀动词 URI 模式控制器#Action

    todo_items GET    /todos/:todo_id/items(.:format)     items#index

       POST   /todos/:todo_id/items(.:format)     items#create


    todo_item GET    /todos/:todo_id/items/:id(.:format) items#show

       PATCH  /todos/:todo_id/items/:id(.:format) items#update

       PUT    /todos/:todo_id/items/:id(.:format) items#update

       DELETE /todos/:todo_id/items/:id(.:format) items#destroy

 todos GET    /todos(.:format)                    todos#index

       POST   /todos(.:format)                    todos#create

  todo GET    /todos/:id(.:format)                todos#show

       PATCH  /todos/:id(.:format)                todos#update

       PUT    /todos/:id(.:format)                todos#update

       DELETE /todos/:id(.:format)                todos#destroy

  auth_login POST   /auth/login(.:format)        authentication#authenticate

signup POST   /signup(.:format)                   users#create

这是authentication_controller.spec:

    require 'rails_helper'

    RSpec.describe AuthenticationController, type: :controller do
      # Authentication test suite
      describe 'POST /auth/login' do
        # create test user
        let!(:user) { create(:user) }

        # set headers for authorization
        let(:headers) { valid_headers.except('Authorization')  }

        # set test valid and invalid credentials
        let(:valid_credentials) do 
           { 
              email: user.email, 
              password: user.password 
           } 
        end # let(:valid_credentials) do 

        let(:invalid_credentials) do 
          {
             email: Faker::Internet.email,
             password: Faker::Internet.password
          }
        end # let(:invalid_credentials) do 

        # set request.headers to our custom headers
        # before { allow(request).to receive(:headers).and_return(headers) }

        # returns auth token when request is valid
        context 'when request is valid' do
          before { post 'auth/login', params:  valid_credentials , headers: headers }

          it 'returns an authentication token' do
            expect(json['auth_token']).not_to be_nil
          end #  it 'returns an authentication token' do
        end #     context 'when request is valid' do

        # returns failure message when request is invalid
        context 'When request is invalid' do
          before { post '/auth/login', params: invalid_credentials, headers: headers }

          it 'returns a failure message' do
            expect(json['message']).to match(/Invalid credentials/)
          end # it 'returns a failure message' do
        end # context 'When request is invalid' do


      end # describe 'POST /auth/login' do

    end # RSpec.describe AuthenticationController, type: :controller do

这里是authentication_controller:

    class AuthenticationController < ApplicationController

      skip_before_action :authorize_request, only: :authenticate
      # return auth token once user is authenticated
      def authenticate
        auth_token = AuthenticateUser.new(auth_params[:email], auth_params[:password]).call
        json_response(auth_token: auth_token)
      end # def authenticate

      private

      def auth_params
        params.permit(:email, :password)
      end

    end

如有任何帮助,我们将不胜感激。提前致谢。

你打错了动作。您已经在身份验证控制器中,因此除了参数之外,您只需要执行以下操作:

post 'authenticate', params:  valid_credentials , headers: headers

您发布的位置必须与控制器的终点匹配,无论您将方法定义为什么,在这种情况下 'authenticate'