Module

Test::Unit::Assertions

Inheritance

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

Constants

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

Methods

Class

Visibility Signature
public use_pp= (value)

Instance

Visibility Signature
public assert (boolean, message=nil)
public assert_block (message="assert_block failed.") {|| ...}
public assert_equal (expected, actual, message=nil)
public assert_in_delta (expected_float, actual_float, delta, message="")
public assert_instance_of (klass, object, message="")
public assert_kind_of (klass, object, message="")
public assert_match (pattern, string, message="")
public assert_nil (object, message="")
public assert_no_match (regexp, string, message="")
public assert_not_equal (expected, actual, message="")
public assert_not_nil (object, message="")
public assert_not_same (expected, actual, message="")
public assert_nothing_raised (*args) {|| ...}
public assert_nothing_thrown (message="", &proc)
public assert_operator (object1, operator, object2, message="")
public assert_raise (*args) {|| ...}
public assert_raises (*args, &block)
public assert_respond_to (object, method, message="")
public assert_same (expected, actual, message="")
public assert_send (send_array, message="")
public assert_throws (expected_symbol, message="", &proc)
public build_message (head, template=nil, *arguments)
public flunk (message="Flunked")

Class Method Detail

use_pp=(value)

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.

Instance Method Detail

assert(boolean, message=nil)

Asserts that boolean is not false or nil.

Example:

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

assert_block(message="assert_block failed.") {|| ...}

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

assert_equal(expected, actual, message=nil)

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

assert_in_delta(expected_float, actual_float, delta, message="")

Passes if expected_float and actual_float are equal within delta tolerance.

Example:

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

assert_instance_of(klass, object, message="")

Passes if object .instance_of? klass

Example:

  assert_instance_of String, 'foo'

assert_kind_of(klass, object, message="")

Passes if object .kind_of? klass

Example:

  assert_kind_of Object, 'foo'

assert_match(pattern, string, message="")

Passes if string =~ pattern.

Example:

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

assert_nil(object, message="")

Passes if object is nil.

Example:

  assert_nil [1, 2].uniq!

assert_no_match(regexp, string, message="")

Passes if regexp !~ string

Example:

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

assert_not_equal(expected, actual, message="")

Passes if expected != actual

Example:

  assert_not_equal 'some string', 5

assert_not_nil(object, message="")

Passes if ! object .nil?

Example:

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

assert_not_same(expected, actual, message="")

Passes if ! actual .equal? expected

Example:

  assert_not_same Object.new, Object.new

assert_nothing_raised(*args) {|| ...}

Passes if block does not raise an exception.

Example:

  assert_nothing_raised do
    [1, 2].uniq
  end

assert_nothing_thrown(message="", &proc)

Passes if block does not throw anything.

Example:

 assert_nothing_thrown do
   [1, 2].uniq
 end

assert_operator(object1, operator, object2, message="")

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

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

Example:

  assert_operator 5, :>=, 4

assert_raise(*args) {|| ...}

Passes if the block raises one of the given exceptions.

Example:

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

assert_raises(*args, &block)

Alias of assert_raise.

Will be deprecated in 1.9, and removed in 2.0.

assert_respond_to(object, method, message="")

Passes if object .respond_to? method

Example:

  assert_respond_to 'bugbear', :slice

assert_same(expected, actual, message="")

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

Example:

  o = Object.new
  assert_same o, o

assert_send(send_array, message="")

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]

assert_throws(expected_symbol, message="", &proc)

Passes if the block throws expected_symbol

Example:

  assert_throws :done do
    throw :done
  end

build_message(head, template=nil, *arguments)

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

flunk(message="Flunked")

Flunk always fails.

Example:

  flunk 'Not done testing yet.'