# Redirect\_to

**URL:** <https://www.ruby-forum.com/t/redirect-to/135141>\
**Category:** RSpec\
**Created:** [April 18, 2008, 11:32am UTC](https://www.ruby-forum.com/t/redirect-to/135141 "2008-04-18T11:32:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 18, 2008, 11:32am UTC](https://www.ruby-forum.com/t/redirect-to/135141/1 "2008-04-18T11:32:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 18, 2008, 11:37am UTC](https://www.ruby-forum.com/t/redirect-to/135141/2 "2008-04-18T11:37:56Z")

</div>

> it “should redirect to the previous page” do  
> response.should be\_redirect  
> end
> 
> Am I missing anything?

do\_delete 🙂

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 18, 2008, 11:39am UTC](https://www.ruby-forum.com/t/redirect-to/135141/3 "2008-04-18T11:39:18Z")

</div>

Pat M. wrote:

> do\_delete 🙂

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

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 18, 2008, 11:43am UTC](https://www.ruby-forum.com/t/redirect-to/135141/4 "2008-04-18T11:43:54Z")

</div>

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](http://rubyforge.org/mailman/listinfo/rspec-users)  
> –  
> Jarkko L.  
> [http://jlaine.net](http://jlaine.net)  
> [http://dotherightthing.com](http://dotherightthing.com)  
> [http://www.railsecommerce.com](http://www.railsecommerce.com)  
> [http://odesign.fi](http://odesign.fi)

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 18, 2008, 12:24pm UTC](https://www.ruby-forum.com/t/redirect-to/135141/5 "2008-04-18T12:24:41Z")

</div>

On 18 Apr 2008, at 10:39, Andy C. 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

–

> **[PatchSpace](http://www.patchspace.co.uk/)**

> **[影视资源聚合平台](http://www.aviewfromafar.net/)**
>
> 为您提供高清电影、电视剧、动漫和综艺内容，支持在线观看与内容筛选。

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 18, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/redirect-to/135141/6 "2008-04-18T12:05:55Z")

</div>

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. 🙂 I reintroduced it in my next ‘write test → fail → write  
code → pass’ cycle.

Thanks to you both!

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [April 18, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/redirect-to/135141/7 "2008-04-18T14:23:57Z")

</div>

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

> On 18 Apr 2008, at 10:39, Andy C. 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?
