Rspec 在控制器测试中找不到路由
Rspec can't find route in controller test
我在使用 Rspec 3 测试 Rails 3.0 应用程序时不断收到路由错误。它无法检测到我用于 get 请求的路由。这是我的文件的样子:
routes.rb
resources :proficiency_tests do
member do
resource :lag_components, only: [:edit, :update]
end
end
运行 rake:routes returns:
edit_lag_components GET /proficiency_tests/:id/lag_components/edit(.:format) {:action=>"edit", :controller=>"lag_components"}
lag_components PUT /proficiency_tests/:id/lag_components(.:format) {:action=>"update", :controller=>"lag_components"}
控制器在:
app
|---controllers
|---lag_components_controller.rb
控制器的设置方式没有什么特别之处。
规格在:
spec
|---controllers
|---lag_components_controller.rb
测试是这样设置的:
需要'spec_helper'
describe LagSubmissionsController do
describe 'GET #edit' do
let(:proficiency_test) { create :proficiency_test }
it 'redirects' do
*tests*
end
end
end
这是我尝试过的方法以及错误消息:
get(:edit, id: proficiency_test.id)
# => No route matches {:id=>11259, :controller=>"lag_submissions", :action=>"edit"}
get(:edit, id: proficiency_test.id, use_route: :edit_lag_components)
# => No route matches {:id=>11260, :controller=>"lag_submissions", :action=>"edit"}
get("/proficiency_tests/#{proficiency_test.id}/lag_components/edit")
# => No route matches {:controller=>"lag_submissions", :action=>"/proficiency_tests/11258/lag_components/edit"}
最后一个特别有趣,因为这是我在浏览器中用来呈现 edit
页面的确切路径:
有谁知道发生了什么事以及为什么找不到那条路线?不幸的是,这是一个较旧的应用程序,我无法更改周围的路线。
路线定义为lag_components,但您的控制器名为LagSubmissions。资源名称需要与控制器名称匹配 - 因此将控制器名称更改为 LagComponentsController。
我在使用 Rspec 3 测试 Rails 3.0 应用程序时不断收到路由错误。它无法检测到我用于 get 请求的路由。这是我的文件的样子:
routes.rb
resources :proficiency_tests do
member do
resource :lag_components, only: [:edit, :update]
end
end
运行 rake:routes returns:
edit_lag_components GET /proficiency_tests/:id/lag_components/edit(.:format) {:action=>"edit", :controller=>"lag_components"}
lag_components PUT /proficiency_tests/:id/lag_components(.:format) {:action=>"update", :controller=>"lag_components"}
控制器在:
app
|---controllers
|---lag_components_controller.rb
控制器的设置方式没有什么特别之处。
规格在:
spec
|---controllers
|---lag_components_controller.rb
测试是这样设置的:
需要'spec_helper'
describe LagSubmissionsController do
describe 'GET #edit' do
let(:proficiency_test) { create :proficiency_test }
it 'redirects' do
*tests*
end
end
end
这是我尝试过的方法以及错误消息:
get(:edit, id: proficiency_test.id)
# => No route matches {:id=>11259, :controller=>"lag_submissions", :action=>"edit"}
get(:edit, id: proficiency_test.id, use_route: :edit_lag_components)
# => No route matches {:id=>11260, :controller=>"lag_submissions", :action=>"edit"}
get("/proficiency_tests/#{proficiency_test.id}/lag_components/edit")
# => No route matches {:controller=>"lag_submissions", :action=>"/proficiency_tests/11258/lag_components/edit"}
最后一个特别有趣,因为这是我在浏览器中用来呈现 edit
页面的确切路径:
有谁知道发生了什么事以及为什么找不到那条路线?不幸的是,这是一个较旧的应用程序,我无法更改周围的路线。
路线定义为lag_components,但您的控制器名为LagSubmissions。资源名称需要与控制器名称匹配 - 因此将控制器名称更改为 LagComponentsController。