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?
Craig Demyanovich
2011-Feb-28 14:21 UTC
[rspec-users] Simple Association Redirect on delete
On Sun, Feb 27, 2011 at 10:57 PM, Charley <charley.stran at gmail.com> wrote: ...> 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?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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110228/da425203/attachment.html>
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?
On Feb 28, 8:21?am, Craig Demyanovich <cdemyanov... at gmail.com>
wrote:> On Sun, Feb 27, 2011 at 10:57 PM, Charley <charley.st... at
gmail.com> wrote:
>
> ...
>
> > 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?
>
> 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
>
> _______________________________________________
> rspec-users mailing list
> rspec-us... at
rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
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. On Feb 28, 7:14?pm, Charley <charley.st... at gmail.com> wrote:> 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? > > On Feb 28, 8:21?am, Craig Demyanovich <cdemyanov... at gmail.com> wrote: > > > > > > > > > On Sun, Feb 27, 2011 at 10:57 PM, Charley <charley.st... at gmail.com> wrote: > > > ... > > > > 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? > > > 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 > > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
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. On Feb 28, 7:14?pm, Charley <charley.st... at gmail.com> wrote:> 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? > > On Feb 28, 8:21?am, Craig Demyanovich <cdemyanov... at gmail.com> wrote: > > > > > > > > > On Sun, Feb 27, 2011 at 10:57 PM, Charley <charley.st... at gmail.com> wrote: > > > ... > > > > 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? > > > 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 > > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users