= TesterXtreme

The Tester Xtreme plugin is a way to distill common testing patterns into easy-to-use macros that are added as class methods to your test cases. There are currently macros for both unit (ActiveRecord model) and functional (ActionController controllers) tests.

== Unit Test Macros

=== Validation Tests

Each of the validation testing macros checks to make sure there is an appropriate validation method defined in the model for each attribute specified.

should_require

Ensures that +validates_presence_of+ was used.

should_require_numeric

Ensures that +validates_numericality_of+ was used.

should_require_unique

Ensures that +validates_uniqueness_of+ was used.

should_protect

Ensures that +attr_protected+ was used for this attribute.

Usage:

class UserTest < ActiveSupport::TestCase should_require :first_name, :last_name should_require_unique :username should_require_numeric :friends_count should_protect :hashed_password end

=== Association Tests

Each of the association testing macros checks to make sure the correct associations were defined on this model. The names map directly to the association definitions:

should_have_many

Ensures that +has_many+ was used.

should_belong_to

Ensures that +belongs_to+ was used.

should_have_one

Ensures that +has_one+ was used.

should_have_and_belong_to_many

Ensures that +has_and_belongs_to_many+ was used.

Usage:

class UserTest < ActiveSupport::TestCase should_have_many :friends should_belong_to :organization should_have_one :credit_card should_have_and_belong_to_many :categories end

== Functional Test Macros

Each of the functional testing macros require that you first request an action on the controller. To accomplish this, the 4 standard actions are implemented as class methods for the test:

  • get
  • post
  • put
  • delete

In addition to simple requests, you can also specify parameters and initial states:

get(:show){ User.expects(:find).returns(mocked_user) }.with(:id => 1) post(:create).with(:username => ‘foobar’)

From here, you can use the macros to define certain expectations on the controller:

should_assign

Asserts that the specified variable is set as an instance variable.

should_not_assign

Asserts that the specified variable is not set as an instance variable.

should_use_filter

Asserts that the designated filter method is run.

should_not_use_filter

Asserts that the designated filter method is not run.

should_render

Asserts that the response for the action is successful and that the specified template is rendered.

should_set_flash

Asserts that the specified key and value was set in the flash collection.

should_set_session

Asserts that the specified variable was set in session.

should_redirect_to

Asserts that the response was a redirect and that the action redirects to the specified route.

Usage:

class UsersController < ActionController::TestCase get(:new).should_render(:new) post(:create).with(:username => ‘foo’).should_redirect_to(:new_user_url) delete(:destroy).should_set_flash(:notice, ‘You must log in’) get(:show).with(:id => 1).should_use_filter(:check_for_current_user) get(:show).with({:id => 1}, {:user_id => 3}).should_assign(:current_user) end

= Credits

Copyright © 2007 Ben Scofield, released under the MIT license