Redirect_to

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.

‘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!

it “should redirect to the previous page” do
response.should be_redirect
end

Am I missing anything?

do_delete :slight_smile:

Pat M. wrote:

do_delete :slight_smile:

I do so love mistakes in public, don’t you?

On 18.4.2008, at 12.32, Andy C. wrote:

@cart = Cart.find(session[:cart], :include => :items) if

session[:cart] = 1000
:items}).and_return(@cart)
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

expected redirect? to return true, got false
http://rubyforge.org/mailman/listinfo/rspec-users

Jarkko L.
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi

On 18 Apr 2008, at 10:39, Andy C. wrote:

do_delete :slight_smile:

I do so love mistakes in public, don’t you?

I do this SO often - hopefully I’m not alone :slight_smile: - 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/

Jarkko L. wrote:

I would perhaps use

it “should redirect to the previous page” do
response.should redirect_to “/prev/page”
end

Yeah, I’d pulled it back to see what really simple mistake I was
making. :slight_smile: I reintroduced it in my next ‘write test -> fail -> write
code -> pass’ cycle.

Thanks to you both!

On Apr 18, 2008, at 6:16 AM, Ashley M. wrote:

On 18 Apr 2008, at 10:39, Andy C. wrote:

do_delete :slight_smile:

I do so love mistakes in public, don’t you?

I do this SO often - hopefully I’m not alone :slight_smile: - 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?

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