2

著者は多くの作品を持っています。作品は作者のものです。

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'か?ビューで機能しますが、コントローラーでも機能するのではないでしょうか。

4

1 に答える 1

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)だけではいけないようで、何か他のものが必要だったようです。stub(:parent) も試してみましたが、うまくいきませんでした。inherited_resources をよく理解していないようです。

コメントは大歓迎です。

于 2011-08-29T18:07:47.507 に答える