RSpecing:嵌套资源的更新操作

时间:2011-08-29 15:57:48

标签: ruby-on-rails-3 bdd rspec2 inherited-resources

作者有很多作品。作品属于作者。

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:show动作?我错过了什么吗?

我正在使用inherited-resources,因此是controller.stub(:resource)。
我也注意到assign(:work, work) throws undefined method 'assign'?它在视图中工作,不应该也适用于控制器吗?

1 个答案:

答案 0 :(得分: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

所以看起来我不能只是stub(:resource),看起来像是需要别的东西。尝试使用存根(:父)也没有运气。看起来我不太了解inherited_resources。

欢迎提出意见。