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-2 of 2 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):
# 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

Modified Growl support for Ruby autotest

# This code is a different approach to using Growl notifications (Mac OS X)
# with autotest for Ruby.
# The code can be added to ~/.autotest
# Be sure to comment out "require 'autotest/growl'" if you have that line
# in your .autotest file.
# Based on ZenTest-3.6.1/lib/autotest/growl.rb
# Requires the 2 rails png graphics found at:
# http://blog.internautdesign.com/2006/11/12/autotest-growl-goodness

module Autotest::Growl
  # save the name of the working directory (e.g., the app name)
  # to be used as a key to identify this particular run of autotest
  # so the same growl notification will be reused for updates,
  # instead of a new growl window for every posting.
  @@current_directory_name = Dir.pwd.match(/([^\/]+)\z/).to_s
  def self.growl title, msg, pri=0
    title += " in #{@@current_directory_name}"
    msg += " at #{Time.now}"
    # ### change the path to the directory for the png graphics:
    system "growlnotify -n autotest #{pri > 0 ? '-s ' : ''}--image /Users/grant/Pictures/rails_#{pri > 0 ? 'fail' : 'ok'}.png -p #{pri} -d '#{Dir.pwd}' -m #{msg.inspect} #{title}"
  end

  Autotest.add_hook :run do  |at|
    growl "autotest running", "Started"
  end
  Autotest.add_hook :red do |at|
    growl "Tests Failed", "#{at.files_to_test.size} tests failed", 2
  end
  Autotest.add_hook :green do |at|
    growl "Tests Passed", "Tests passed", -2 if at.tainted
  end
  Autotest.add_hook :all_good do |at|
    growl "Tests Passed", "All tests passed", -2 if at.tainted
  end
end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS