1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Test Driven Development in Ruby--The Tests

By

Making Tests Pass in Test Driven Development

Remember, only write enough code to make the test pass, no more. Expanding on the example shown in Writing Code Using Test Driven Development we have only one test; it expects a NameParser class to return John Smith and to be initialized with a string. Well, that's pretty easy, just make full_name return "John Smith". The class would look something like the following class. Save this code in a file called nameParser.rb. Any future modifications to it will be highlighted in bold, be sure to make the same modifications to your file.

class NameParser
  def initialize(n)
  end

  def full_name
    "John Smith"
  end
end

Make a small modification to tc_nameParser.rb, add the following line to the top, before the existing require statement. It simply requires the file with out NameParser class in it.

require 'nameParser.rb'

Run the tests and to see that yes, they do pass.

>ruby tc_nameParser.rb
Loaded suite tc_nameParser
Started
.
Finished in 0.0 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

But What I Really Meant to Say Was

I know what you're thinking--I'm cheating. I'm being lazy and not doing any parsing at all! What I'm really doing is only doing enough to make the tests pass. The tests haven't said I should be doing any parsing, or even that I should be storing the name, so I haven't written that part yet. So, this should really be storing the name. Let's write a test to reflect that.

def test_name_stored
  n1 = NameParser.new( "John Smith" )
  n2 = NameParser.new( "Alice Smith" )

  assert_equal( "John Smith", n1.full_name )
  assert_equal( "Alice Smith", n2.full_name )
end

This is straightforward, as all the tests should be. It creates two objects with different names and make sure the names are returned correctly. Note that this test doesn't use the object created by setup. Not all tests will, but most do. So run the test and watch it fail.

>ruby tc_nameParser.rb
Loaded suite tc_nameParser
Started
.F
Finished in 0.094 seconds.

  1) Failure:
test_name_stored(TestNameParser) [tc_nameParser.rb:19]:
<"Alice Smith"> expected but was
<"John Smith">.

2 tests, 3 assertions, 1 failures, 0 errors

It wanted Alice Smith, but got the hard-coded response. Let's fix this by making the constructor store the name and the full_name method return it. That's the minimum needed to make the test pass.

class NameParser
  def initialize(n)
    <b>@name = n</b>
  end

  def full_name
    <b>@name</b>
  end
end

Run the tests again and make sure they all pass.

>ruby tc_nameParser.rb
Loaded suite tc_nameParser
Started
..
Finished in 0.0 seconds.

2 tests, 3 assertions, 0 failures, 0 errors

Zero failures, everything is good. Time to move on.

  1. About.com
  2. Technology
  3. Ruby
  4. Advanced Ruby
  5. Test-Driven Development
  6. Test Driven Development in Ruby--The Tests

©2014 About.com. All rights reserved.