Question

Author has many works. Work belongs to author.

resources :authors do
  resources :works
end

And the 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

And the error:

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>

Why it doesn't redirect to works_controller's :show action? Am I missing something?

I'm using inherited-resources hence controller.stub(:resource).
I've also noticed that assign(:work, work) throws undefined method 'assign'? It works in views, shouldn't it work for controllers too?

Was it helpful?

Solution

This is what did the trick.

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

So it seems I can't just stub(:resource), looks like something else was needed. Tried with stub(:parent) also with no luck. Looks like I don't understand inherited_resources so good.

Comments are welcome.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top