[rspec-users] redirect_to

Andy Croll lists at ruby-forum.com
Fri Apr 18 05:32:41 EDT 2008


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/.


More information about the rspec-users mailing list