# Helper for testing default routes generated by a resource

**URL:** <https://www.ruby-forum.com/t/helper-for-testing-default-routes-generated-by-a-resource/114261>\
**Category:** Rails\
**Created:** [September 10, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/helper-for-testing-default-routes-generated-by-a-resource/114261 "2007-09-10T17:46:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 10, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/helper-for-testing-default-routes-generated-by-a-resource/114261/1 "2007-09-10T17:46:56Z")

</div>

Snippet posted to: [http://snippets.dzone.com/posts/show/4517](http://snippets.dzone.com/posts/show/4517)

These methods test that the routes for resources defined in routes.rb  
are working as expected. Call them from your functional (controller)  
tests.

Add the following 3 methods to test/test\_helper.rb:

# Test for routes generated by map.resource (singular).

def assert\_routing\_for\_resource(controller, skip=[], nesting=[])  
routes = [  
[“new”,‘/new’,{},:get], [“create”,‘’,{},:post],  
[“show”,‘’,{},:get], [“edit”,‘;edit’,{},:get],  
[“update”,‘’,{},:put], [“destroy”,‘’,{},:delete]  
]  
check\_resource\_routing(controller, routes, skip, nesting)  
end

# Test for routes generated by map.resources (plural).

def assert\_routing\_for\_resources(controller, skip=[], nesting=[])  
routes = [  
[“index”,‘’,{},:get], [“new”,‘/new’,{},:get], [“create”,‘’,  
{},:post],  
[“show”,‘/1’,{:id=\>‘1’},:get], [“edit”,‘/1;edit’,{:id=\>‘1’},:get],  
[“update”,‘/1’,{:id=\>‘1’},:put], [“destroy”,‘/1’,  
{:id=\>‘1’},:delete]  
]  
check\_resource\_routing(controller, routes, skip, nesting)  
end

# Check that the expected paths will be generated by a resource, and

that

# the expected params will be generated by paths defined by a

resource.

# routes is array of [action, url string after controller, extra

params].  
def check\_resource\_routing(controller, routes, skip=[], nesting=[])

# set a prefix for nested resources

prefix = nesting.join(‘s/1/’)  
unless prefix.blank?  
prefix += “s/1/”  
end

# Add params for nested resources.

# For each ‘nest’, include a “:nest\_id=\>‘1’” param.

params = {}  
nesting.each do |param|  
params[“#{param}\_id”.to\_sym] = ‘1’  
end

# Test each of the standard resource routes.

routes.each do |pair|  
unless skip.include? pair[0]  
assert\_generates(“/#{prefix}#{controller}#{pair[1]}”,  
{:controller=\>controller,  
:action=\>pair[0]}.merge(pair[2]).merge(params), {}, {},  
“Failed generation of resource route for action #{pair[0]} /  
#{prefix}#{controller}#{pair[1]}”)  
assert\_recognizes(  
{:controller=\>controller,  
:action=\>pair[0]}.merge(pair[2]).merge(params),  
{:path=\>“/  
#{prefix}#{controller}#{pair[1]}”, :method=\>pair[3]},  
{}, “Failed to recognize resource route for path #{pair[3]}😕  
#{prefix}#{controller}#{pair[1]}”)  
end  
end  
end

EXAMPLES

You can specify actions to ‘skip’ (if you have a special route for  
that action).  
If using nested resources, set the nesting array (use singular  
strings).

So, if you have the following in routes.rb:  
map.make\_thing ‘/make’, :controller=\>‘things’, :action=\>‘new’  
map.resources :nests do |nest|  
nest.resources :things  
end  
map.resource :foo

then you can use the following in things\_controller\_test.rb:  
def test\_resource\_routing  
assert\_routing\_for\_resources ‘things’, [‘new’], [‘nest’]  
assert\_routing\_for\_resource ‘foo’  
end
