# Unit Testing of Routes

**URL:** <https://www.ruby-forum.com/t/unit-testing-of-routes/175547>\
**Category:** Rails\
**Created:** [September 15, 2009, 1:25pm UTC](https://www.ruby-forum.com/t/unit-testing-of-routes/175547 "2009-09-15T13:25:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![icke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@icke](https://www.ruby-forum.com/u/icke)\
**Post date:** [September 15, 2009, 1:25pm UTC](https://www.ruby-forum.com/t/unit-testing-of-routes/175547/1 "2009-09-15T13:25:33Z")

</div>

Hi,

I am trying to pull together a unit testing for routes but I am  
getting confused. I have read AWDWR section 21.4 which explains how  
to code the assertions.

> **[Testing Rails Applications — Ruby on Rails Guides](https://guides.rubyonrails.org/testing.html#testing-routes)**
>
> Testing Rails ApplicationsThis guide covers built-in mechanisms in Rails for testing your application.After reading this guide, you will know: Rails testing terminology. How to write unit, functional, integration, and system tests for your...

also basically says the same. The problem I have is working out what  
else I need in my test/unit/test\_routing.rb.

require File.dirname( **FILE** ) + ‘/…/test\_helper’

def test\_recognizes  
ActionController::Routing.use\_controllers! [“quote”]  
load “config/routes.rb”  
test “internal routing” do  
assert\_recognizes({“controller” =\> “quote” , “action” =\>  
“index” }, “/quote” )  
assert\_recognizes({“controller” =\> “quote” , “action” =\>  
“new” }, “/” )  
end  
end

def test\_routing  
true  
end

The output is the following:

$ ruby -I development test/unit/routing\_test.rb  
Loaded suite test/unit/routing\_test  
Started

Finished in 0.000182 seconds.

0 tests, 0 assertions, 0 failures, 0 errors

Agreed it is super fast, but there appears to be no testing going on.  
It’s early days on my testing on Rails so I am sure I am missing  
something major here. Thanks for any pointers in the right direction.

O.

---

<div class="post-metadata">

**Author:** ![icke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@icke](https://www.ruby-forum.com/u/icke)\
**Post date:** [September 15, 2009, 5:17pm UTC](https://www.ruby-forum.com/t/unit-testing-of-routes/175547/2 "2009-09-15T17:17:21Z")

</div>

The example in the guide does the testing in a functional test, where  
the routing system is already loaded. I’d recommend that you do the  
same.

–Matt J.

---

<div class="post-metadata">

**Author:** ![icke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@icke](https://www.ruby-forum.com/u/icke)\
**Post date:** [September 15, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/unit-testing-of-routes/175547/3 "2009-09-15T17:47:47Z")

</div>

You need to put those test methods into a Test::Unit::TestCase…

On Sep 15, 2009, at 7:23 AM, icke wrote:

> Hi,
> 
> I am trying to pull together a unit testing for routes but I am  
> getting confused. I have read AWDWR section 21.4 which explains how  
> to code the assertions. [Testing Rails Applications — Ruby on Rails Guides](http://guides.rubyonrails.org/testing.html#testing-routes)  
> also basically says the same. The problem I have is working out what  
> else I need in my test/unit/test\_routing.rb.

Change the file name from test\_routing.rb to routing\_test.rb (the rake  
task to run the tests is probably looking for \*\_test.rb). Your  
specific run of the file with ruby is actually fine, however.

I typically put these into test/functional/…\_test.rb because they  
relate to controllers (and functional tests are really “unit tests for  
controllers”).

> require File.dirname( **FILE** ) + ‘/…/test\_helper’

class RoutingTest \< Test::Unit::TestCase

> 

# get rid of this do-nothing test

> def test\_routing  
> true  
> end  
> end

> Agreed it is super fast, but there appears to be no testing going on.  
> It’s early days on my testing on Rails so I am sure I am missing  
> something major here. Thanks for any pointers in the right direction.
> 
> O.

I would similarly avoid testing more than one route per test. I tend  
to only bother with routing tests when they relate to AJAX callbacks  
or otherwise represent literal URL strings in JavaScript. Your tests  
seem to be verifying that you are using Rails routing properly – not  
that it is wrong, but it should be more for your learning that a habit  
that you continue once you understand how to create routes.

Here’s some actual code from the test/functional/  
similarity\_controller\_test.rb on one of my projects (but only some  
parts that are testing routes). (And be aware that this project is  
_OLD_ as in Rails 1.2.6 so investigate how to do the same thing in  
your particular version of Rails. In particular, I know that setup is  
completely different in Rails 2.x.)

require File.dirname( **FILE** ) + ‘/…/test\_helper’  
require ‘similarity\_controller’

# Re-raise errors caught by the controller.

class SimilarityController; def rescue\_action(e) raise e end; end

class SimilarityControllerTest \< Test::Unit::TestCase  
fixtures :users, :categories, :products

def setup  
@controller = SimilarityController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new  
end

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

# vendor/rails/actionpack/lib/action\_controller/assertions/

routing\_assertions.rb

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

def test\_\_route\_similarity\_trait\_groups  
assert\_routing ‘/similarity/1/groups’, { :controller =\>  
‘similarity’, :action =\> ‘trait\_groups’, :id =\> ‘1’ }  
end

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

#…

# 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

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

def test\_\_route\_similarity\_remove  
assert\_recognizes({ :controller =\> ‘similarity’, :action =\>  
‘remove’,  
:id =\> ‘1’, :trait\_group =\> ‘2’, :follower =\>  
‘4’ },  
{ :path =\> ‘/similarity/1/group/2/remove/  
4’, :method =\> :delete })

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

```

‘remove’,  
:id =\> ‘1’, :trait\_group =\> ‘2’, :follower =\>  
‘4’ })  
end

#…  
end

I hope that helps you.

-Rob

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

---

<div class="post-metadata">

**Author:** ![icke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@icke](https://www.ruby-forum.com/u/icke)\
**Post date:** [September 15, 2009, 8:33pm UTC](https://www.ruby-forum.com/t/unit-testing-of-routes/175547/4 "2009-09-15T20:33:27Z")

</div>

Rob and Matt,

Thanks for your reply. I have now got my head around this now, and  
have now traced where my routing problem is and now have an reusable  
test to check I don’t break things by changing routes.rb in future.  
Rob, having a real-life example was what I was really looking for in  
the documentation, I always find that more useful.

Here is my new ‘functional’ test in full as a reference for future to  
just get the basic stuff going.

# test/functional/routing\_test.rb

require File.dirname( **FILE** ) + ‘/…/test\_helper’

class RoutingTest \< ActionController::TestCase

def setup  
load “config/routes.rb”  
end

def test\_recognizes\_quote\_routes  
ActionController::Routing.use\_controllers! [“quote”]  
assert\_recognizes({:controller =\> ‘quote’ , :action =\> ‘index’ },  
“/quote” )  
assert\_recognizes({:controller =\> ‘quote’ , :action =\> ‘new’ },  
“/” )  
assert\_recognizes({:controller =\> ‘quote’ , :action =\>  
‘affiliate’, :affiliate =\> ‘xxxx’}, “/xxxx” )  
assert\_recognizes({:controller =\> ‘quote’ , :action =\>  
‘promotion’, :affiliate =\> ‘scan’, :promotion =\> ‘promo’}, “/scan/  
promo” )  
end

def test\_recognizes\_policyholder  
ActionController::Routing.use\_controllers! [“policyholder”]  
assert\_recognizes({:controller =\> ‘policyholder’ , :action =\>  
‘new’ }, “/policyholder/new” )  
assert\_recognizes({:controller =\> ‘policyholder’ , :action =\>  
‘edit’, :id =\> ‘3’ }, “/policyholder/edit/3” )  
end

def test\_recognizes\_confirmation  
ActionController::Routing.use\_controllers! [“confirmation”]  
assert\_recognizes({:controller =\> ‘confirmation’ , :action =\>  
‘new’ }, “/confirmation/new” )  
end

end

– and to run it.

$ ruby test/functional/routing\_test.rb  
Loaded suite test/functional/routing\_test  
Started  
…  
Finished in 0.157376 seconds.

3 tests, 7 assertions, 0 failures, 0 errors

Thanks again.

O.
