public
Description: test helpers to concisely assert pre-conditions and post-conditions
Homepage:
Clone URL: git://github.com/ndp/assert_changes.git
name age message
file .gitignore Thu May 14 00:07:11 -0700 2009 Added actual files [ndp]
file README Thu May 14 00:19:16 -0700 2009 add some text [ndp]
file README.textile Thu May 14 01:29:37 -0700 2009 Learning how to format the readme [ndp]
directory lib/ Thu May 14 00:07:11 -0700 2009 Added actual files [ndp]
directory test/ Thu May 14 00:07:11 -0700 2009 Added actual files [ndp]
README.textile

Assert Changes Test Helpers

assert_changes is a more general version of assert_difference test helper.
Whereas assert_difference is numerical only, and can only assert that integer
counts have changed, assert_changes evaluates pre- and post- conditions for
any ruby type. It can evaluate boolean expressions, as well as Strings or
any objects that can evaluated for equality.

Mix into your test or test_helper to write more concise tests.

assert_changes

Assert that a any Ruby expression changes. Instead of:

    assert a != 'world'
    a = 'world'
    assert_equal 'world', a

Write DRY:
    include AssertChanges
    ...
    assert_changes 'a' do
        a = 'world'
    end

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”.

Assert that several things change by passing an array:

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

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

Sometimes you don’t care about the original value, such as a value from the
fixture, but you need to make sure it starts out different than the final
result (otherwise the test is invalid). Pass just the final value.

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

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 is a modified version of this. It has slightly 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