rails rspec没有路线匹配 [英] rails rspec No route matches
本文介绍了rails rspec没有路线匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试测试我的路线(它们对于实际请求而言正常工作)并收到此错误:
I'm trying to test my routes (they work fine for real requests) and get this error:
ActionController::UrlGenerationError:
No route matches {:action=>"api/login", :controller=>"login", :email=>"a@a.com", :password=>"super_secure"}
我看过很多类似的问题,但找不到确切的答案我的问题。
I've looked at a bunch of similar questions but could not find a precise answer for my problem.
我的路线是:
Rails.application.routes.draw do
post 'api/login' => 'login#email_login'
我的测试是
describe LoginController do
it 'works fine' do
password = 'super_secure'
salt = BCrypt::Engine.generate_salt.to_s
hash_password = BCrypt::Engine.hash_secret(password, salt).to_s
user_login = FactoryGirl.create(:user_login, user_hash: hash_password, salt: salt, email: 'a@a.com')
post 'api/login', password: password, email: user_login.email
expect(response).to be_success
end
end
最后,我的 LoginController
是:
class LoginController < ActionController::Base
protect_from_forgery except: [:email_login], with: :exception
def email_login
auth = UserLogin.auth(params[:email], params[:password])
return render text: 'unauthorized', status: 401 unless auth[:user]
return render json: auth
end
end
我尝试了多种方法将数据传递给post方法,但是它们都失败了...
I've tried different ways to pass the data to the post method but they all failed...
您有一个线索,对您有帮助!
It you have a clue, that would help!
推荐答案
在测试中,尝试调整路径,就像这样:
In your test, try adjusting your path, like this:
post '/api/login', password: password, email: user_login.email
这篇关于rails rspec没有路线匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文