Simple Association Redirect on delete

So am fairly new to rspec and am trying to get the hang of it, but
have run into a few issues I cant figure out. Several times in my code
I have model that belongs to a parent, when the model is deleted,
instead of redirecting to the index action, it redirects to the show
action for its parent model. Simple enough. The code in my application
currently properly handles this, but I dont know how to make the test
handle it. For example I have this basic auto-generated test.

it “redirects to the bugs list” do
Bug.stub(:find) { mock_bug }
delete :destroy, :id => “1”
response.should redirect_to(bugs_url)
end

This fails for obvious reasons. A bug belongs to a project so the
redirect should go to the projects_url(“someproject_id_I_dont_know”)

I am sure this is a fairly simple and common thing. Can anyone help
put me on the right path?

On Sun, Feb 27, 2011 at 10:57 PM, Charley [email protected]
wrote:

put me on the right path?
Make the mock_bug respond to project_id. Then verify that the request’s
response goes to project_path(:id => mock_bug.project_id).

Regards,
Craig

I am not sure if I am properly using the respond to call as you
suggested. Here is what I tried:
it “redirects to the bugs parent project” do
Bug.stub(:find) { mock_bug }
mock_bug.should respond_to :project_id
delete :destroy, :id => “1”
response.should redirect_to(project_path(:id =>
mock_bug.project_id))
end

And here is the failure message:

  1. BugsController DELETE destroy redirects to the bugs parent project
    Failure/Error: delete :destroy, :id => “1”
    ActionController::RoutingError:
    No route matches
    {:action=>“show”, :controller=>“projects”, :id=>#<Bug:0x514c9fe
    @name=“Bug_1015”>}

In case it would illustrate my situation better, here is my action:
def destroy
@bug = Bug.find(params[:id])
bugs_project = @bug.project_id
@bug.destroy

respond_to do |format|
  format.html { redirect_to(project_url(bugs_project)) }
  format.xml  { head :ok }
end

end

Any ideas?

Here is how I ended up getting my test to pass:
instead of using a stub/mock, I used a Factory, since that more
closely aligned with the actual functionality anyway.

I’m having the (seemingly) exact same issue. Were you able to figure
this out? I’m relatively new to rspec and my controller code works,
but the test is failing. It seems like it is not able to retrieve a
method from the stub.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs