[rspec-users] redirect_to

Jarkko Laine jarkko at jlaine.net
Fri Apr 18 05:41:03 EDT 2008


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
>  end

I would perhaps use

>  it "should redirect to the previous page" do
>    response.should redirect_to "/prev/page"
>  end


since 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 


More information about the rspec-users mailing list