问题
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.
My route is:
Rails.application.routes.draw do
post 'api/login' => 'login#email_login'
and my test is
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
Finally my LoginController
is:
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
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!
回答1:
In your test, try adjusting your path, like this:
post '/api/login', password: password, email: user_login.email
来源:https://stackoverflow.com/questions/28690290/rails-rspec-no-route-matches