2011-08-29 71 views
2

作者有很多作品。作品屬於作者。RSpecing:嵌套資源的更新操作

resources :authors do 
    resources :works 
end 

而RSpec的:

it "should redirect to :show" do 
    work = FactoryGirl.create(:work) 
    controller.stub(:resource) { work } 
    work.should_receive(:update_attributes) { true } 

    put :update, id: work.id, author_id: work.author.id, work: {} 

    response.should redirect_to(admin_author_work_path(work.author, work)) 
end 

和錯誤:

Expected response to be a redirect to <http://test.host/admin/authors/353/works/166> 
but was a redirect to <http://test.host/admin/authors/353/works> 

爲什麼不能重定向到works_controller的:表演動作?我錯過了什麼嗎?

我正在使用inherited-resources,因此controller.stub(:resource)。我也注意到assign(:work, work) throws undefined method 'assign'?它在視圖中工作,它不應該也適用於控制器嗎?

回答

1

這就是訣竅。

it "should also redirect to :show" do 
    author = mock_model(Author) 
    work = mock_model(Work) 

    Author.stub(:find) { author } 
    author.stub_chain(:works, :find) { work } 

    work.should_receive(:update_attributes) { true } 

    put :update, id: author.id, author_id: work.id, work: {} 

    response.should redirect_to(admin_author_work_path(author, work)) 
end 

所以看來我不能只存根(:資源),看起來像別的東西是需要的。試圖與存根(:父母)也沒有運氣。看起來我不太瞭解inherited_resources這麼好。

歡迎評論。