Lake Denman

Exploring Ruby: Test/Unit - assert method

The ruby koans library provides an about_asserts.rb koan which is the inspiration for this. The ruby koans are a fun way to learn more about ruby and programming.

 
## We shall contemplate truth by testing reality, via asserts.
assert true

The assert method tests whether an object, the true object in the example above, is true or false. Since the true object is true, assert returns true.

Implementation Details

assert is a method defined in the Test/Unit library, which is part of the ruby standard libraries. The Test::Unit::Assertions#assert method ends up delegating to the MiniTest::Assertions#assert method.

require 'test/unit'
class TestAssertion < Test::Unit::TestCase
  def test_wrong_assertion
    error, line = assert_raise(ArgumentError) {assert(true, true)}, __LINE__
    assert_match(/assertion message must be String or Proc, but TrueClass was given/, error.message)
    assert_match(/\A#{Regexp.quote(__FILE__)}:#{line}:/, error.backtrace[0])
  end
end

 

# /home/me/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/test/unit/assertions.rb

require 'minitest/unit'

module Test
  module Unit
    module Assertions
      include MiniTest::Assertions

      def assert(test, msg = (nomsg = true; nil))
        unless nomsg or msg.instance_of?(String) or msg.instance_of?(Proc) or
            (bt = caller).first.rindex(MiniTest::MINI_DIR, 0)
          bt.delete_if {|s| s.rindex(MiniTest::MINI_DIR, 0)}
          raise ArgumentError, "assertion message must be 
            String or Proc, but #{msg.class} was given.", bt
        end
        super
      end
    end
  end
end

 

 
# /home/me/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/minitest/unit.rb
# Fails unless +test+ is a true value.

module MiniTest
  module Assertions
    def assert test, msg = nil
      msg ||= "Failed assertion, no message given."
      self._assertions += 1
      unless test then
        msg = msg.call if Proc === msg
        raise MiniTest::Assertion, msg
      end
      true
    end
  end
end

Asserting equality

Using assert, it is possible to test equality of objects. The examples are run from irb, so requiring “test/unit” and including the Test:Unit::Assertions module is required to use the assert method.

require 'test/unit'
include Test::Unit::Assertions

> assert 5 == 5
#=> true

> assert 5 == 4
#=> MiniTest::Assertion: Failed assertion, no message given.

> assert 5 == 4, "Five equals Five, silly!"
#=> MiniTest::Assertion: Five equals Five, silly!

There is a better method for testing equality called assert_equal. It displays more appropriate information on failure. The assert_equal method delagates to assert method.

> assert_equal 5,5
#=> true
> assert_equal 1,2
#=> MiniTest::Assertion: Expected 1, not 2.

A Simple Unit Test

The assert method is really made for unit testing.

require 'test/unit'

class Person
  attr_reader :age
  def initialize
    @age = 0
  end
end

class TestPerson < Test::Unit::TestCase
  def test_new_person_age_is_zero
    assert(Person.new.age.zero?)
  end
end

Running the test above yields the following output:

Loaded suite test
Started
.
Finished in 0.000272 seconds.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

Conclusion

The assert method can get you very far as far as unit testing is concerned.

You may be interested in the assert method used in other places, check out: Assert in C

Home