# Best practices for testing Routes?

**URL:** <https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367>\
**Category:** Rails\
**Created:** [December 17, 2007, 4:35am UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367 "2007-12-17T04:35:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@ryan\_s](https://www.ruby-forum.com/u/ryan_s)\
**Post date:** [December 17, 2007, 4:35am UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367/1 "2007-12-17T04:35:45Z")

</div>

I’m wondering if there are any accepted practices for testing routes  
in rails. For instance, if I added a route like:

map.root :controller =\> ‘welcome’

And I wanted to add an assertion like:

assert\_routing ‘’, :controller =\> ‘welcome’, :action =\> ‘index’

Where should tests like this be placed? It seems like they could go  
in a routes.rb testing class, but would that go in the functional  
tests folder or in the integration folder? Or a test could be added  
to the welcome controller’s tests, since it’s the one that should  
handle the request.

I’ve been looking around online, and looking through some open source  
rails apps, but I haven’t found an answer yet. Is there a right place  
for this?

---

<div class="post-metadata">

**Author:** ![ryan\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@ryan\_s](https://www.ruby-forum.com/u/ryan_s)\
**Post date:** [December 17, 2007, 4:58am UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367/2 "2007-12-17T04:58:00Z")

</div>

Why would you want to test that? There are already tests written for  
that  
(in the rails code itself), map.root :controller =\> “welcome” will  
always go  
to :controller =\> “welcome”, :action =\> “index”

On Dec 17, 2007 2:05 PM, ryan\_s [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Where should tests like this be placed? It seems like they could go
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![ryan\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@ryan\_s](https://www.ruby-forum.com/u/ryan_s)\
**Post date:** [December 17, 2007, 5:20am UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367/3 "2007-12-17T05:20:13Z")

</div>

Maybe I’m being overly defensive in my testing.

I tend to take the perspective that there should be a test for  
anything that I expect the application to do. That way the tests can  
act as a specification for what is expected of the app, and also they  
guard against me doing something stupid in the future, like adding a  
higher priority route that preempts the route I’m testing.

---

<div class="post-metadata">

**Author:** ![ryan\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@ryan\_s](https://www.ruby-forum.com/u/ryan_s)\
**Post date:** [December 17, 2007, 5:42am UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367/4 "2007-12-17T05:42:25Z")

</div>

As I said before there are already tests for this. You shouldn’t be  
testing  
the routing unless you’re changing the actual code of it.

Testing something like whether or not a page redirects somewhere is  
recommended, but going as far as to test something that’s core Rails  
stuff,  
I wouldn’t bother testing it. It should always do what you think it  
should  
do, unless you’re not using a stable release.

---

<div class="post-metadata">

**Author:** ![ryan\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@ryan\_s](https://www.ruby-forum.com/u/ryan_s)\
**Post date:** [December 17, 2007, 6:38am UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367/5 "2007-12-17T06:38:30Z")

</div>

I was trying to provide the simplest example possible to avoid forcing  
people to read too much, but I’m afraid my example might have been too  
simple. I don’t intend to test that “map.root” works, instead I want  
to test that my application routes requests the way I want it to.

Here is a more in depth example of the sort of tests I’m thinking of.  
Say I wanted to create a blog like the one described in example 2 of  
the Rails Introduction to Routes page at  
[Peak Obsession](http://manuals.rubyonrails.com/read/chapter/65).

If I approached this using test driven methodology I would write a  
test and then add a route to fulfill that test. So, when I added my  
posts/show/10 route there would have been a test proving that it  
worked. Later when I added the new route from the example:

map.connect ‘:year/:month/:day’, :controller =\> ‘blog’, :action =\>  
‘by\_date’

I would immediately notice that my posts/show/10 test failed. Then it  
wouldn’t take long to realize that the new route was too vague, and it  
was matching the posts route when it shouldn’t. So, I could get to  
the better route from the example with less effort:

map.connect ‘date/:year/:month/:day’, :controller =\> ‘blog’, :action  
=\> ‘by\_date’,  
:month =\> nil, :day =\> nil,  
:requirements =\> {:year =\> /\d{4}/, :day =\> /  
\d{1,2}/, :month =\> /\d{1,2}/}

Without tests to validate my routes I might not have noticed this  
mistake as quickly, and once I did notice it might take me longer to  
find the problem if I had made other changes between adding the  
problem route and noticing the problem.

I realize not everyone uses TDD, but I’m sure some Rails developers  
do. I’m wondering if any test first Rails people out there have any  
advice on where to stick tests of this nature.

---

<div class="post-metadata">

**Author:** ![ryan\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@ryan\_s](https://www.ruby-forum.com/u/ryan_s)\
**Post date:** [December 17, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367/6 "2007-12-17T14:11:13Z")

</div>

On Dec 17, 2007, at 12:37 AM, ryan\_s wrote:

> If I approached this using test driven methodology I would write a  
> the better route from the example with less effort:  
> problem route and noticing the problem.
> 
> > Testing something like whether or not a page redirects somewhere is  
> > recommended, but going as far as to test something that’s core  
> > Rails stuff,  
> > I wouldn’t bother testing it. It should always do what you think it  
> > should  
> > do, unless you’re not using a stable release.

ryan\_s,  
I tend to put these tests in the same file that tests the controller  
to which the route is connected. For the example in this message,  
that’d be in test/functional/blog\_controller\_test.rb. But know that  
there are two forms that your testing needs to take.

For a GET route, it’s as simple as (I always put a test that the  
controller name is right, too):

def test\_\_controller\_naming  
assert\_equal(‘similarity’, @controller.controller\_name,  
“None of the routing tests will work if  
controller\_name is not ‘similarity’”)  
end

# for a non-standard default action

def test\_\_route\_similarity\_categories  
assert\_routing ‘/similarity’, { :controller =\>  
‘similarity’, :action =\> ‘categories’ }  
end

# for a nested-ish resource

def test\_\_route\_similarity\_candidates  
assert\_routing(‘/similarity/1/group/2’,  
{ :controller =\> ‘similarity’, :action =\> ‘list’,  
:id =\> ‘1’, :trait\_group =\> ‘2’ })  
end

However, if you want to test that the specific kind of request is  
used, you need to use the two separate methods that assert\_routing  
uses internally because there’s no way to pass all the needed options  
through.

# Need to split these out in order to require that POST is used.

def test\_\_route\_similarity\_make\_similar  
assert\_recognizes({ :controller =\> ‘similarity’, :action =\>  
‘make\_similar’,  
:id =\> ‘1’, :trait\_group =\> ‘2’, :leader =\>  
‘3’, :follower =\> ‘4’ },  
{ :path =\> ‘/similarity/1/group/2/3/4’, :method  
=\> :post })

```
 assert_generates('/similarity/1/group/2/3/4',
                  { :controller => 'similarity', :action =>

```

‘make\_similar’,  
:id =\> ‘1’, :trait\_group =\> ‘2’, :leader =\>  
‘3’, :follower =\> ‘4’ })  
end

This route happens to be for an AJAX’d action so less fluff in the URL  
wasn’t a concern.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ryan\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@ryan\_s](https://www.ruby-forum.com/u/ryan_s)\
**Post date:** [December 18, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367/7 "2007-12-18T16:57:58Z")

</div>

On 12/17/07, ryan\_s [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was trying to provide the simplest example possible to avoid forcing  
> people to read too much, but I’m afraid my example might have been too  
> simple. I don’t intend to test that “map.root” works, instead I want  
> to test that my application routes requests the way I want it to.

The way I think about this is that I’m testing that what I put in my  
config/routes.rb file does what I think it should.

For those using RSpec, the latest version makes two specs for a  
controller if you use the “script/generate rspec\_scaffold foo” command

specs  
controllers  
foos\_controller\_spec.rb  
foos\_routing\_spec.rb

The foos\_controller\_spec.rb corresponds roughly to the test case for  
the controller in the test/integration directory and contains  
specifications of the behavior of the controller.

The foos\_routing\_spec.rb contains specs for routes pertaining to the  
FoosController.

I was quite skeptical about Rspec when I first learned of it a year  
ago, but it’s winning me over.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ryan\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@ryan\_s](https://www.ruby-forum.com/u/ryan_s)\
**Post date:** [December 18, 2007, 3:22am UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367/8 "2007-12-18T03:22:31Z")

</div>

Thanks Rob, this is just the sort of information I was looking for.

Ryan

On Dec 17, 7:10 am, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ryan\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@ryan\_s](https://www.ruby-forum.com/u/ryan_s)\
**Post date:** [December 19, 2007, 5:49am UTC](https://www.ruby-forum.com/t/best-practices-for-testing-routes/123367/9 "2007-12-19T05:49:10Z")

</div>

That’s interesting Rick. I’ve been meaning to look into RSpec for a  
while now, but this gave me the impetus to actually do it. It looks  
to me like a nice way of providing more verbose tests. I think I’ll  
give it a try and see how I like it. Thanks for the tip.

Ryan
