Module Test::Unit::Assertions
In: lib/test/unit/assertions.rb

Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is included in Test::Unit::TestCase.

To include it in your own code and use its functionality, you simply need to rescue Test::Unit::AssertionFailedError. Additionally you may override add_assertion to get notified whenever an assertion is made.

Notes:

  • The message to each assertion, if given, will be propagated with the failure.
  • It is easy to add your own assertions based on assert_block().

Example Custom Assertion

  def deny(boolean, message = nil)
    message = build_message message, '<?> is not false or nil.', boolean
    assert_block message do
      not boolean
    end
  end

Methods

Constants

UncaughtThrow = {NameError => /^uncaught throw \`(.+)\'$/, ThreadError => /^uncaught throw \`(.+)\' in thread /}

Public Class methods

Select whether or not to use the pretty-printer. If this option is set to false before any assertions are made, pp.rb will not be required.

Public Instance methods

Asserts that boolean is not false or nil.

Example:

  assert [1, 2].include?(5)

The assertion upon which all other assertions are based. Passes if the block yields true.

Example:

  assert_block "Couldn't do the thing" do
    do_the_thing
  end

Passes if expected == +actual.

Note that the ordering of arguments is important, since a helpful error message is generated when this one fails that tells you the values of expected and actual.

Example:

  assert_equal 'MY STRING', 'my string'.upcase

Passes if expected_float and actual_float are equal within delta tolerance.

Example:

  assert_in_delta 0.05, (50000.0 / 10**6), 0.00001

Passes if object .instance_of? klass

Example:

  assert_instance_of String, 'foo'

Passes if object .kind_of? klass

Example:

  assert_kind_of Object, 'foo'

Passes if string =~ pattern.

Example:

  assert_match(/\d+/, 'five, 6, seven')

Passes if object is nil.

Example:

  assert_nil [1, 2].uniq!

Passes if regexp !~ string

Example:

  assert_no_match(/two/, 'one 2 three')

Passes if expected != actual

Example:

  assert_not_equal 'some string', 5

Passes if ! object .nil?

Example:

  assert_not_nil '1 two 3'.sub!(/two/, '2')

Passes if ! actual .equal? expected

Example:

  assert_not_same Object.new, Object.new

Passes if block does not raise an exception.

Example:

  assert_nothing_raised do
    [1, 2].uniq
  end

Passes if block does not throw anything.

Example:

 assert_nothing_thrown do
   [1, 2].uniq
 end

Compares the +object1+ with +object2+ using operator.

Passes if object1.send(operator, object2) is true.

Example:

  assert_operator 5, :>=, 4

Passes if the block raises one of the given exceptions.

Example:

  assert_raise RuntimeError, LoadError do
    raise 'Boom!!!'
  end

Alias of assert_raise.

Will be deprecated in 1.9, and removed in 2.0.

Passes if object .respond_to? method

Example:

  assert_respond_to 'bugbear', :slice

Passes if actual .equal? expected (i.e. they are the same instance).

Example:

  o = Object.new
  assert_same o, o

Passes if the method send returns a true value.

send_array is composed of:

  • A receiver
  • A method
  • Arguments to the method

Example:

  assert_send [[1, 2], :include?, 4]

Passes if the block throws expected_symbol

Example:

  assert_throws :done do
    throw :done
  end

Builds a failure message. head is added before the template and arguments replaces the ’?’s positionally in the template.

Flunk always fails.

Example:

  flunk 'Not done testing yet.'

[Validate]