Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Grant Neufeld http://grantneufeld.ca/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Helper for testing default routes generated by a resource in Ruby on Rails

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 (updated for Rails 1.2.5 which no longer uses semicolons as a separator for the edit action):
   1  
   2  # Test for routes generated by map.resource (singular).
   3  def assert_routing_for_resource(controller, skip=[], nesting=[])
   4    routes = [
   5      ["new",'/new',{},:get], ["create",'',{},:post],
   6      ["show",'',{},:get], ["edit",'/edit',{},:get],
   7      ["update",'',{},:put], ["destroy",'',{},:delete]
   8      ]
   9    check_resource_routing(controller, routes, skip, nesting)
  10  end
  11  # Test for routes generated by map.resources (plural).
  12  def assert_routing_for_resources(controller, skip=[], nesting=[])
  13    routes = [
  14      ["index",'',{},:get], ["new",'/new',{},:get], ["create",'',{},:post],
  15      ["show",'/1',{:id=>'1'},:get], ["edit",'/1/edit',{:id=>'1'},:get],
  16      ["update",'/1',{:id=>'1'},:put], ["destroy",'/1',{:id=>'1'},:delete]
  17      ]
  18    check_resource_routing(controller, routes, skip, nesting)
  19  end
  20  
  21  # Check that the expected paths will be generated by a resource, and that
  22  # the expected params will be generated by paths defined by a resource.
  23  # routes is array of [action, url string after controller, extra params].
  24  def check_resource_routing(controller, routes, skip=[], nesting=[])
  25    # set a prefix for nested resources
  26    prefix = nesting.join('s/1/')
  27    unless prefix.blank?
  28      prefix += "s/1/"
  29    end
  30    # Add params for nested resources.
  31    # For each 'nest', include a ":nest_id=>'1'" param.
  32    params = {}
  33    nesting.each do |param|
  34      params["#{param}_id".to_sym] = '1'
  35    end
  36    # Test each of the standard resource routes.
  37    routes.each do |pair|
  38      unless skip.include? pair[0]
  39        assert_generates("/#{prefix}#{controller}#{pair[1]}",
  40          {:controller=>controller,
  41          :action=>pair[0]}.merge(pair[2]).merge(params), {}, {},
  42          "Failed generation of resource route for action #{pair[0]} /#{prefix}#{controller}#{pair[1]}")
  43        assert_recognizes(
  44          {:controller=>controller,
  45            :action=>pair[0]}.merge(pair[2]).merge(params),
  46          {:path=>"/#{prefix}#{controller}#{pair[1]}", :method=>pair[3]},
  47          {}, "Failed to recognize resource route for path #{pair[3]}:/#{prefix}#{controller}#{pair[1]}")
  48      end
  49    end
  50  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:
   1  
   2  map.make_thing '/make', :controller=>'things', :action=>'new'
   3  map.resources :nests do |nest|
   4    nest.resources :things
   5  end
   6  map.resource :foo

then you can use the following in things_controller_test.rb:
   1  
   2  def test_resource_routing
   3    assert_routing_for_resources 'things', ['new'], ['nest']
   4    assert_routing_for_resource 'foo'
   5  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS