[ next ] [ prev ] [ contents ] [ skip to Code after Story One ] XP-Cinti TDD Workshop

Some Loose Ends

We are pretty close do done at this point. Let's handle some loose ends.

Duplication has creeped into our test suit. Duplication is the bane of all programmers and we try to eliminate it wherever we can. In this case, we notice that all of our tests follow this pattern...

class TestNet < Test::Unit::TestCase
  def test_untied
    net = Net.new
    ...
  end

  def test_tie
    net = Net.new
    ...
  end

  def test_multiple_positions
    net = Net.new
    ...
  end

  def test_untie
    net = Net.new
    ...
  end
end

All of our tests begin with the line net = Net.new. Using the setup method of a test case, we can move this duplication to one spot. The net variable now becomes an instance variable (@net) and onerous duplication is removed.

The test code is now...

class TestNet < Test::Unit::TestCase
  def setup
    @net = Net.new
  end

  def test_untied
    ... # Changes references from net to @net here
  end

  def test_tie
    ... # Changes references from net to @net here
  end

  def test_multiple_positions
    ... # Changes references from net to @net here
  end

  def test_untie
    ... # Changes references from net to @net here
  end
end

We run our tests and they all pass.


[ next ] [ prev ] [ contents ] [ skip to Code after Story One ] Copyright 2003 by Jim Weirich.
Some Rights Reserved