# Testing response for named routes in integration tests

**URL:** <https://www.ruby-forum.com/t/testing-response-for-named-routes-in-integration-tests/182902>\
**Category:** Rails\
**Created:** [February 4, 2010, 3:25pm UTC](https://www.ruby-forum.com/t/testing-response-for-named-routes-in-integration-tests/182902 "2010-02-04T15:25:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [February 4, 2010, 3:25pm UTC](https://www.ruby-forum.com/t/testing-response-for-named-routes-in-integration-tests/182902/1 "2010-02-04T15:25:45Z")

</div>

In integration tests, you can test the response of the server given a  
url\_for\_options hash, like so:  
get(:action, :param1 =\> “one”, :param2 =\> “two”, …)  
assert\_response :success

However, this doesn’t work with a named route.

# routes.rb

map.thing\_download “things/:id/download” {:controller  
=\> :things, :action =\> :download}

# things\_controller\_test.rb:

get(:download, :id =\> @thing.id) #=\> ActionController::RoutingError,  
no route matches {:controller =\> :things, :action =\> :download, :id =\>  
12}  
get(thing\_download\_path(@thing)) #=\> ActionController::RoutingError,  
no route matches {:controller =\> :things, :action =\> “/things/12/  
download”}

Is there a way to get a response from a named route in functional  
tests? For the time being I’ve resorted to misusing integration tests  
for this purpose, because inheriting from  
ActionController::IntegrationTest gives you a get() function that  
takes a string, so I can do  
get(thing\_download\_path(@thing))

---

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [February 4, 2010, 4:12pm UTC](https://www.ruby-forum.com/t/testing-response-for-named-routes-in-integration-tests/182902/2 "2010-02-04T16:12:08Z")

</div>

Adam S. wrote:

> In integration tests, you can test the response of the server given a  
> url\_for\_options hash, like so:  
> get(:action, :param1 =\> “one”, :param2 =\> “two”, …)  
> assert\_response :success
> 
> However, this doesn’t work with a named route.
> 
> # routes.rb
> 
> map.thing\_download “things/:id/download” {:controller  
> =\> :things, :action =\> :download}
> 
> # things\_controller\_test.rb:
> 
> get(:download, :id =\> @thing.id) #=\> ActionController::RoutingError,  
> no route matches {:controller =\> :things, :action =\> :download, :id =\>  
> 12}  
> get(thing\_download\_path(@thing)) #=\> ActionController::RoutingError,  
> no route matches {:controller =\> :things, :action =\> “/things/12/  
> download”}
> 
> Is there a way to get a response from a named route in functional  
> tests?

Are you looking for assert\_recognizes ?

More to the point, should you care? Routing is UI-facing stuff, so  
(even more than elsewhere) you should be testing behavior, not URL  
options.

> For the time being I’ve resorted to misusing integration tests  
> for this purpose, because inheriting from  
> ActionController::IntegrationTest gives you a get() function that  
> takes a string, so I can do  
> get(thing\_download\_path(@thing))

Don’t bother with integration tests. Use Cucumber.

(Also consider RSpec – it’s a lot nicer than Test::Unit.)

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [February 4, 2010, 6:16pm UTC](https://www.ruby-forum.com/t/testing-response-for-named-routes-in-integration-tests/182902/3 "2010-02-04T18:16:06Z")

</div>

On Feb 4, 9:12 am, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Are you looking for assert\_recognizes ?
> 
> More to the point, should you care? Routing is UI-facing stuff, so  
> (even more than elsewhere) you should be testing behavior, not URL  
> options.  
> I’m not testing the URL, I’m testing the behavior of the application  
> when the user hits that URL, under different conditions. Under certain  
> conditions it should be 403 Forbidden, while under others it’s 200 OK.

> Don’t bother with integration tests. Use Cucumber.
> 
> (Also consider RSpec – it’s a lot nicer than Test::Unit.)  
> I’ll look into those, thanks.

---

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [February 4, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/testing-response-for-named-routes-in-integration-tests/182902/4 "2010-02-04T18:33:08Z")

</div>

Adam S. wrote:

> On Feb 4, 9:12ï¿½am, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Are you looking for assert\_recognizes ?
> > 
> > More to the point, should you care? ï¿½Routing is UI-facing stuff, so  
> > (even more than elsewhere) you should be testing behavior, not URL  
> > options.  
> > I’m not testing the URL, I’m testing the behavior of the application  
> > when the user hits that URL, under different conditions. Under certain  
> > conditions it should be 403 Forbidden, while under others it’s 200 OK.

Right – so you don’t need to worry about the actual path or params.  
You just need to test the response.

I know how to do that in Cucumber, but that uses Webrat’s visit method.  
I’m not sure about integration tests.

> > Don’t bother with integration tests. ï¿½Use Cucumber.
> > 
> > (Also consider RSpec – it’s a lot nicer than Test::Unit.)  
> > I’ll look into those, thanks.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
