OK I''m back and surely missing stuff again... In my controller tests I''m checking for a redirect after a destroy action. First up it''s a single resource ("map.resource :cart" in routes.rb) in CartsController.rb def destroy @cart = Cart.find(session[:cart], :include => :items) if session[:cart] if @cart Cart.delete(@cart.id) session[:cart] = nil end redirect_to :back end in the spec: (note I''m also testing the case where the is no session var) describe CartsController, "empty existing cart (destroy in db)" do before(:each) do @item1 = mock_model(CartItem, :id => 1) @item2 = mock_model(CartItem, :id => 2) @cart = mock_model(Cart, :id => 1) @cart.stub!(:items).and_return([@item1, @item2]) Cart.stub!(:find).and_return(@cart) session[:cart] = 1000 request.env["HTTP_REFERER"] = "/prev/page" end def do_delete delete :destroy end it "should look in the session for a cart" do Cart.should_receive(:find).with(session[:cart], {:include => :items}).and_return(@cart) do_delete end it "should delete the cart" do Cart.should_receive(:delete).with(1).and_return(true) do_delete end it "should delete the cart id from the session" do do_delete session[:cart].should == nil end it "should redirect to the previous page" do response.should be_redirect end end Unfortunately I''m getting this. 1) ''CartsController empty existing cart (destroy in db) should redirect to the previous page'' FAILED expected redirect? to return true, got false Which just ain''t true! Am I missing anything? I''d also like to hear any comment on my spec ''style'' as I''m new! -- Posted via http://www.ruby-forum.com/.
> it "should redirect to the previous page" do > response.should be_redirect > end > > > Am I missing anything?do_delete :)
Pat Maddox wrote:> do_delete :)I do so love mistakes in public, don''t you? -- Posted via http://www.ruby-forum.com/.
On 18.4.2008, at 12.32, Andy Croll wrote:> OK I''m back and surely missing stuff again... > > In my controller tests I''m checking for a redirect after a destroy > action. > > First up it''s a single resource ("map.resource :cart" in routes.rb) > > in CartsController.rb > > def destroy > @cart = Cart.find(session[:cart], :include => :items) if > session[:cart] > if @cart > Cart.delete(@cart.id) > session[:cart] = nil > end > redirect_to :back > end > > in the spec: (note I''m also testing the case where the is no session > var) > > describe CartsController, "empty existing cart (destroy in db)" do > > before(:each) do > @item1 = mock_model(CartItem, :id => 1) > @item2 = mock_model(CartItem, :id => 2) > > @cart = mock_model(Cart, :id => 1) > @cart.stub!(:items).and_return([@item1, @item2]) > > Cart.stub!(:find).and_return(@cart) > session[:cart] = 1000 > > request.env["HTTP_REFERER"] = "/prev/page" > end > > def do_delete > delete :destroy > end > > it "should look in the session for a cart" do > Cart.should_receive(:find).with(session[:cart], {:include => > :items}).and_return(@cart) > do_delete > end > > it "should delete the cart" do > Cart.should_receive(:delete).with(1).and_return(true) > do_delete > end > > it "should delete the cart id from the session" do > do_delete > session[:cart].should == nil > end > > it "should redirect to the previous page" do > response.should be_redirect > endI would perhaps use> it "should redirect to the previous page" do > response.should redirect_to "/prev/page" > endsince you''re also interested in where the redirect goes, but dunno if it solves your problem. //jarkko> > > end > > Unfortunately I''m getting this. > > 1) > ''CartsController empty existing cart (destroy in db) should redirect > to > the previous page'' FAILED > expected redirect? to return true, got false > > Which just ain''t true! > > Am I missing anything? I''d also like to hear any comment on my spec > ''style'' as I''m new! > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2417 bytes Desc: not available Url : http://rubyforge.org/pipermail/rspec-users/attachments/20080418/d5b5c0b6/attachment.bin
Jarkko Laine wrote:> I would perhaps use > >> it "should redirect to the previous page" do >> response.should redirect_to "/prev/page" >> endYeah, I''d pulled it back to see what *really* simple mistake I was making. :-) I reintroduced it in my next ''write test -> fail -> write code -> pass'' cycle. Thanks to you both! -- Posted via http://www.ruby-forum.com/.
On 18 Apr 2008, at 10:39, Andy Croll wrote:>> do_delete :) > > I do so love mistakes in public, don''t you?I do this SO often - hopefully I''m not alone :) - I am starting to wish RSpec would ask me "Didn''t you mean to make a request in this example?" I''m used to it now though... it''s usually the first thing I check in controller specs. Ashley -- http://www.patchspace.co.uk/ http://aviewfromafar.net/
On Apr 18, 2008, at 6:16 AM, Ashley Moran wrote:> On 18 Apr 2008, at 10:39, Andy Croll wrote: >>> do_delete :) >> >> I do so love mistakes in public, don''t you? > > I do this SO often - hopefully I''m not alone :) - I am starting to > wish RSpec would ask me "Didn''t you mean to make a request in this > example?"That''s actually been discussed recently - Pat - where did we leave that?> I''m used to it now though... it''s usually the first thing I check in > controller specs. > > Ashley