rails rspec No route matches [duplicate]

给你一囗甜甜゛ 提交于 2020-01-03 05:10:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!