2011-09-19 98 views
5

我想在Rails中爲嵌套資源創建測試。相關的路由定義是:用RSpec測試嵌套資源

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的嵌套資源。我在這裏做一些根本性的錯誤嗎?

更新:耙路由的相關部分是:

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

你可以發佈什麼耙路線| grep社區給你? – corroded

+0

更新爲原始問題。 – Sami

+2

這是一個奇怪的ID。你能說明工廠定義中發生了什麼嗎? – zetetic

回答

3

我看到你傳遞content.community.id爲:community_id和物體看起來像被識別的蒙戈文件與BSON :: ObjectId。嘗試使用to_param代替如下:

get :show, :community_id => content.community.to_param, :id => content.to_param