Ruby on rails 尽管浏览器中的路由加载正常,自定义Rails路由测试仍失败
尽管能够通过浏览器成功加载控制器路由,但我在测试控制器路由时遇到错误。铁路4+rspec 有什么想法吗Ruby on rails 尽管浏览器中的路由加载正常,自定义Rails路由测试仍失败,ruby-on-rails,rspec,controller,routes,Ruby On Rails,Rspec,Controller,Routes,尽管能够通过浏览器成功加载控制器路由,但我在测试控制器路由时遇到错误。铁路4+rspec 有什么想法吗 #controller spec describe PublicSitesController do describe "GET index" do it "returns success" do get :index #line 7 in the spec file response.status.shou
#controller spec
describe PublicSitesController do
describe "GET index" do
it "returns success" do
get :index #line 7 in the spec file
response.status.should == 200
end
end
end
#routes
get ":site_name/:page_name", to: "public_sites#show"
get ":site_name", to: 'public_sites#index'
get "/", to: 'public_sites#root'
#controller
class PublicSitesController < ApplicationController
def root
end
def index
end
def show
end
end
#the error:
Failures:
1) PublicSitesController GET index returns success
Failure/Error: get :index
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"public_sites"}
# ./spec/controllers/public_sites_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
请求中缺少一些参数,路由器不知道如何处理“:site_name”,请尝试以下操作:
get :index, site_name: 'something'
编辑:
当您在测试中调用get/post/etc时,您使用该方法调用操作名,而不是url,这样控制器测试就独立于使该操作工作的url(您可以更改url,控制器仍将工作)
您的路由告诉rails它需要一个名为“site\u name”的参数,因此您需要用一个操作参数告诉rails“site\u name”中的内容
如果您想进行路由测试,您可以在那里测试某个url是否指向某个控制器的操作,并在某个参数上显示某个值
当您在浏览器上打开站点时,您没有调用该操作,实际上您正在运行整个应用程序,然后路由系统调用控制器的操作
编辑2:
如果你想测试show动作,你应该用
get :show, site_name: 'some_site', page_name: 'some_page'
复制“rake ROTES”的输出添加rake ROTES输出作为请求可以尝试
get'/public\u sites/index'
,查看路由中的:site\u name符号是否可以在测试范围内“解析”为其值谢谢,这使测试通过。但是,get'/the site name'
会导致没有路由匹配{:controller=>“public_sites”,:action=>“/站点名称/页面名称”}
。。。而浏览器测试则显示正确的视图。你能详细解释一下吗?谢谢。我通过了路由测试{:get=>“/site name/page name”}。应该路由到(“public_sites#show”,site_name:“site name”,page_name:“page name”),这就是为什么情况如此混乱的部分原因。
get :show, site_name: 'some_site', page_name: 'some_page'