【发布时间】:2016-04-29 20:54:54
【问题描述】:
因此,我创建了一个小型模型/控制器来处理我的应用程序中的令牌,并带有自定义路由。但是,当我运行测试(rspec)时,我会失败
相关代码
控制器
读取令牌(在别处生成)
class TokensController < ApplicationController
def read_token
#do whatever this token is supposed to do (unsubscribe, confirm email, etc)
redirect_to(root_path)
end
end
路线
get '/ta/:uuid' => 'tokens#read_token', param: :uuid, as: 'token_action'
rake 路线产生
token_action GET /ta/:uuid(.:format) tokens#read_token {:param=>:uuid}
型号
包括在创建链接时显示令牌使用的是uuid,而不是id
class Token < ActiveRecord::Base
def to_param
uuid
end
end
它在开发模式下工作! :D
但是当我运行测试时……
测试
rspec 控制器测试
require 'rails_helper'
RSpec.describe TokensController, type: :controller do
describe "#index" do
it "test" do
get :read_token, uuid: "12345" #12345 is obviously not real
expect(response).to redirect_to(root_path)
end
end
end
错误信息
ActionController::UrlGenerationError:
No route matches` {:action=>"read_token", :controller=>"tokens", :uuid=>"12345"}
我尝试了什么
嗯……几乎所有东西。
- 我尽我所能写了路线
- 我将
:uuid更改为:id认为这可能是问题所在 - 我尝试以不同的方式指定属性,但无济于事
- 我在 stackoverflow 上尝试了所有类似问题的解决方案
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 rspec routing