Collection of useful test helpers for Ruby/Rails tests
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
script
test
.gitignore
.rakeTasks
History.txt
Manifest.txt
PostInstall.txt
README.textile
Rakefile
VERSION
ayudante.gemspec
ayudante.iml
ayudante.ipr
ayudante.iws
init.rb

README.textile

ayudante

DESCRIPTION:

Collection of useful helpers for writing unit/functional/integration testunit tests.

FEATURES/PROBLEMS:

Fixture Helpers

This uses method_missing to provide additional functions for each of your
fixtures:

assert_list_of_N

This asserts that the lists are the same size and have the same items. Any
difference is reported.

Example:

fixtures :candy_bars
assert_list_of_candy_bars [:almond_joys, :m_and_ms], CandyBar.find_nutty()

assert_set_of_N

This asserts that the lists are the same size and have the same items, but order
is ignored.

Example:

assert_set_of_candy_bars [:almond_joys, :m_and_ms], CandyBar.find_nutty()

assert_contains_N

This asserts that all the items are found in the second parameter, but order
is ignored.

Example:

assert_contains_candy_bars [:almond_joys, :m_and_ms], CandyBar.find_nutty()

Pre and Post Condition Checks

assert_changes

Write:

    assert_changes 'a' do
        a = 'world'
    end

instead of:
    assert a != 'world'
    a = 'world'
    assert_equal 'world', a

The string 'a' passed to assert changes is evaluated in the block context, both before and after the block is run. The first eval is call the pre-condition, and the second the post-condition.

Be explicit about a state change by specifying both the starting and ending values using an expression pointing to array of before and after values:

    o.answer = 'yes'
    assert_changes 'o.answer' => ['yes','no'] do
      o.answer = 'no'
    end

When given one value, it is considered the post-condition value.
The precondition is that the value does NOT equal it:

    i = true
    assert_changes 'i' => false do   # read as: i changes to false
      i = false
    end

Assert that several things change by passing an array:

    a,b = 'hello','hi'
    assert_changes ['a','b'] do
        a = 'world'
        b = 'earth'
    end

Or use a hash, and pass multiple pre/post conditions of arbitrary complexity:

      assert_changes 'post(:a).status' => [:preview, :published],
                            'comment(:c).status' => [:preview, :deleted] do
        ...
      end

assert_no_changes

assert_no_changes has an extended parameter possibilities:

    i,j = 'hello','hi'
    assert_no_changes 'i' do ...                # i (before) == i (after)
    assert_no_changes 'i'=>'hello' do ...       # i == 'hello' before and after
    assert_no_changes ['i','j'] do ...          # neither i nor j change
    assert_no_changes 'i'=>'hello','j'=>'hi' do # or be explicit with multiple

A Complex Example

Provide multiple assertions of arbitrary complexity, referencing fixtures, etc.
Note that assert_changes supports the :no_change symbol:

    assert_changes
        'inotech.services.public.include?(categories(:a))' => [true, :no_change],
            'inotech.services.public.include?(categories(:b))'=>false,
            'inotech.services.public.include?(categories(:c))'=>false do
      post :edit_services_dialog, :id=>inotech.id, :service_category_id=>categories(:a).id
    end

REQUIREMENTS:

  • none

INSTALL:

  • Installing the gem:
    sudo gem install ndp-ayudante --source=http://gems.github.com
  • or in environment.rb
      Rails::Initializer.run do |config|
        config.gem "ndp-ayudante", :source => "http://gems.github.com", :lib=>'ayudante'
      end

TO COME

  • Better error messages on fixture helpers
  • Add assert_includes_N fixture helper
  • Add assert_excludes_N fixture helper

LICENSE:

(The MIT License)

Copyright © 2009 Andrew J. Peterson, ndp@mac.com

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
‘Software’), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.