# How to spec a controller method that involves an rjs redirect

**URL:** <https://www.ruby-forum.com/t/how-to-spec-a-controller-method-that-involves-an-rjs-redirect/156762>\
**Category:** RSpec\
**Created:** [January 2, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/how-to-spec-a-controller-method-that-involves-an-rjs-redirect/156762 "2009-01-02T17:03:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nasir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@nasir](https://www.ruby-forum.com/u/nasir)\
**Post date:** [January 2, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/how-to-spec-a-controller-method-that-involves-an-rjs-redirect/156762/1 "2009-01-02T17:03:18Z")

</div>

Hi everyone,

What is the best way to spec a controller method that involves an rjs  
redirect? Something like this

def index  
Â url\_to = @parent ? auction\_bids\_path(@parent)Â : auctions\_path  
Â render :update do |page|  
Â Â Â page.redirect\_to(url\_to)  
Â end  
end

I tried  
Â response.should have\_rjs  
for the redirect but it didnt work. Any help would be appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![nasir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@nasir](https://www.ruby-forum.com/u/nasir)\
**Post date:** [January 3, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/how-to-spec-a-controller-method-that-involves-an-rjs-redirect/156762/2 "2009-01-03T21:33:29Z")

</div>

On Fri, Jan 2, 2009 at 10:01 AM, Nasir J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> I tried  
> response.should have\_rjs  
> for the redirect but it didnt work. Any help would be appreciated.

Please be more specific when you say “it didn’t work.” What precisely  
was the code in the spec and what precisely was the error you got.

---

<div class="post-metadata">

**Author:** ![nasir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@nasir](https://www.ruby-forum.com/u/nasir)\
**Post date:** [January 4, 2009, 5:18am UTC](https://www.ruby-forum.com/t/how-to-spec-a-controller-method-that-involves-an-rjs-redirect/156762/3 "2009-01-04T05:18:06Z")

</div>

On Fri, Jan 2, 2009 at 11:01 AM, Nasir J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the best way to spec a controller method that involves an rjs  
> redirect? Something like this

Consider:

response.should =~ /window.location.href = ‘(whatever your expected  
path is)’;/

I didn’t confirm that regex, obviously. But the concept is pretty  
simple: if you know how the RJS redirect works and what Javascript is  
supposed to be generated, then test for its presence in the response.  
If it’s there, your controller logic worked.

–  
Have Fun,  
Steve E. ([removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid))  
ESCAPE POD - The Science Fiction Podcast Magazine  
[http://www.escapepod.org](http://www.escapepod.org)
