为什么我得到“ActionController :: UrlGenerationError:没有路由匹配...”错误?

时间:2018-05-08 14:56:52

标签: ruby-on-rails ruby actioncontroller

我正在使用带有rails 5.0.7的ruby 2.3.1。运行RSpec时出现以下错误:

故障:

1)AuthenticationController POST / auth / login在请求有效时返回一个身份验证令牌      失败/错误:在{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当请求无效时,返回失败消息      失败/错误:在{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

将'auth / login'发布到:'authentication#authenticate'

将'注册'发布到:'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

任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:3)

你正在采取错误的行动。您已经在身份验证控制器中,所以除了参数之外,您只需要执行操作:

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

您发布的地点必须与控制器的终点相匹配,无论您将方法定义为什么,在本例中为“authenticate”