2011-08-29 90 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这么好。

欢迎评论。