Auto-params plugin

March 5th, 2007 PluginsRails

This plugin allows functional tests to declare parameters that will always be included in get, post, put, delete, and head calls.

This can be especially useful in functional tests involving nested resources.

Install

./script/plugin install http://svn.depixelate.com/plugins/auto_params

How it works

Consider testing a ContactsController given the following routes:

# routes.rb
map.resources :regions do |regions|
  regions.resources :contacts
end

Example controller code:

class ContactsControllerTest < Test::Unit::TestCase
  fixtures :regions, :contacts

  def test_index
    get :index, :region_id => 1
    ...
  end

  def test_create
    post :create, :region_id => 1, :contact => { :name => 'John' }
    ...
  end

end

Example controller code with plugin:

class ContactsControllerTest < Test::Unit::TestCase
  fixtures :regions, :contacts
  auto_params :region_id => 1

  def test_index
    get :index
    ...
  end

  def test_create
    post :create, :contact => { :name => 'John' }
    ...
  end

end

To override an auto_param, just specify it like always:

class ContactsControllerTest < Test::Unit::TestCase
  fixtures :regions, :contacts
  auto_param :region_id => 1

  def test_index
    # automatically call 'get' method with region_id => 1
    get :index
    ...
  end

  def test_create
    # call 'get' method with region_id => 2
    post :create, :region_id => 2, :contact => { :name => 'John' }
    ...
  end
end
--- --- ---

Commenting is closed for this article.