Sam*_*ami 5 ruby-on-rails nested-resources rspec-rails factory-bot
我试图在Rails中为嵌套资源创建测试.相关的路线定义是:
resources :communities do
resources :contents, :type => 'Content'
end
Run Code Online (Sandbox Code Playgroud)
使用RSpec和factory_girl,我试图开始测试,例如
describe ContentsController do
it 'should display a content item under a community' do
content = FactoryGirl.create(:content)
get :show, :community_id => content.community.id, :id => content.id
end
end
Run Code Online (Sandbox Code Playgroud)
这些请求总是导致
Failure/Error: get :show, :community_id => content.community.id, :id => content.id
ActionController::RoutingError:
No route matches {:community_id=>BSON::ObjectId('4e7773c6ac54c3d1ad000002'),
:id=>BSON::ObjectId('4e7773c6ac54c3d1ad000001'), :controller=>"contents",
:action=>"show"}
Run Code Online (Sandbox Code Playgroud)
对于我的生活,我找不到使用RSpec指定到嵌套资源的路径的方法.我在这里做了一些根本错误的事吗?
更新:佣金路线的相关部分是:
community_contents GET /communities/:community_id/contents(.:format) {:action=>"index", :controller=>"contents"}
POST /communities/:community_id/contents(.:format) {:action=>"create", :controller=>"contents"}
new_community_content GET /communities/:community_id/contents/new(.:format) {:action=>"new", :controller=>"contents"}
edit_community_content GET /communities/:community_id/contents/:id/edit(.:format) {:action=>"edit", :controller=>"contents"}
community_content GET /communities/:community_id/contents/:id(.:format) {:action=>"show", :controller=>"contents"}
PUT /communities/:community_id/contents/:id(.:format) {:action=>"update", :controller=>"contents"}
DELETE /communities/:community_id/contents/:id(.:format) {:action=>"destroy", :controller=>"contents"}
Run Code Online (Sandbox Code Playgroud)
我看到您将 content.community.id 作为 :community_id 传递,并且该对象看起来像用 BSON::ObjectId 标识的 mongo 文档。尝试使用 to_param 代替,如下所示:
get :show, :community_id => content.community.to_param, :id => content.to_param
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1775 次 |
| 最近记录: |