【发布时间】:2015-09-16 18:55:57
【问题描述】:
在使用 Rspec 3 测试 Rails 3.0 应用程序时,我不断收到路由错误。它无法检测到我用于获取请求的路由。这是我的文件的样子:
routes.rb
resources :proficiency_tests do
member do
resource :lag_components, only: [:edit, :update]
end
end
运行 rake:routes 返回:
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页面的确切路径:
有谁知道发生了什么以及为什么找不到那条路线?不幸的是,这是一个较旧的应用程序,我无法更改周围的路线。
【问题讨论】:
-
你为错误的控制器编写了 rspec
标签: ruby-on-rails ruby-on-rails-3 rspec rspec3