[ next ] [ prev ] [ contents ] [ skip to Test Number Three -- Tying the Knot ] XP-Cinti TDD Workshop

A Real Test

We will start be writing a test that will force us to write code. Let's start with a test for the is_untied method. The Ruby convention for boolean queries is to append a question mark to the name and to omit the "is_" portion. We will follow that convention here.

Here is our first bit of unit test code.

# file: testnet.rb
require 'test/unit'

class TestNet < Test::Unit::TestCase
  def test_untied
    net = Net.new
    assert_equal true, net.untied?(1,1)
  end
end

Ruby Comments: Methods in Ruby are introduced with the def keyword. Here we define a test named test_untied. This test method creates a Net object by sending the Net class a new message (e.g. Net.new). The net object is stored in a variable called net. There is no need to declare the variable.

The second line asserts that the result of sending our net object a message called untied? is true. The question mark is part of the method name; this is a common Ruby idiom for methods that return true/false.

Let's run our tests ...

  
$ ruby testnet.rb
Loaded suite testnet
Started...
..
Error occurred in test_untied(TestNet): NameError: uninitialized constant Net at TestNet
	testnet.rb:5:in `test_untied'
	testnet.rb:4

Finished in 0.043322 seconds.
1 runs, 0 assertions, 0 failures, 1 errors

Oops! It seems Net is not yet defined. That should be easy to fix.


[ next ] [ prev ] [ contents ] [ skip to Test Number Three -- Tying the Knot ] Copyright 2003 by Jim Weirich.
Some Rights Reserved