5

我正在尝试为 Rails 中的嵌套资源创建测试。相关路由定义为:

resources :communities do
  resources :contents, :type => 'Content'
end

使用 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

这些请求总是导致

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"}

对于我的一生,我找不到使用 RSpec 指定到嵌套资源的路由的方法。我在这里做一些根本错误的事情吗?

更新: rake 路线的相关部分是:

    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"}
4

1 回答 1

3

我看到您将 content.community.id 作为 :community_id 传递,并且该对象看起来像一个用 BSON::ObjectId 标识的 mongo 文档。尝试使用 to_param 代替如下:

get :show, :community_id => content.community.to_param, :id => content.to_param
于 2012-01-01T20:19:36.427 回答