我正在尝试测试一个端点,它不是rails中提供的其余部分之一,(i.e: #index, #new, #edit, #update, #create, #destroy
)
在我的routes.rb
文件中:
get 'hospitals/:id/doctors' => 'hospitals#our_doctors'
从我的hospitals_controller_spec.rb
文件中:
describe "GET #our_doctors" do
before do
get :our_doctors
end
end
但我遇到以下错误:
ActionController::UrlGenerationError: No route matches {:action=>"our_doctors", :controller=>"hospitals"}
如何让我的规范遵循所需的路线?
我也尝试将其称为:
get "/hospitals/#{@hospital.id}/doctors"
但是收到以下错误:
ActionController::UrlGenerationError: No route matches {:action=>"/hospitals/1/doctors", :controller=>"hospitals"}
感谢所有帮助。谢谢。
答案 0 :(得分:0)
如何命名您的路线:
get 'hospitals/:id/doctors' => 'hospitals#our_doctors', as: :our_doctors
如果这不起作用,您可以指定规范使用的路线,如下所示:
describe "GET #our_doctors" do
before do
get :our_doctors, use_route: :our_doctors
end
end
答案 1 :(得分:0)
使用use_route的上述解决方案是临时的,因为您可能会收到弃用警告(取决于您的rspec-rails版本)。下面是我尝试使用该方法时该警告的摘录:
...不推荐在功能测试中传递use_route
选项。 process
方法支持此选项(以及相关的get
,head
,post
,patch
,put
和{{1} } helper)将在下一版本中删除,无需替换。功能测试基本上是控制器的单元测试,他们不应该知道如何配置应用程序的路由。相反,您应该明确地将适当的参数传递给delete
方法......
正如所建议的那样,对于后代,我会在您的情况下执行以下操作以避免不受支持/弃用语法的任何错误:
process
注意:如果我错了,请纠正我,但上述情况假定您想要让所有医生都在特定医院下。
一般来说,我会建议所有非RESTful收集路线测试如下:
get :our_doctors, id: [id_of_hospital]
那应该没问题。希望这很有帮助。