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

Test Driven Development in Ruby -- Writing Code


Writing Code Using Test Driven Development

It's one thing to understand the concept of Test Driven Development, but it's another to start writing code using it. The best way to do that is to see it in progress. Here, the task is to write a class that parses names. Given a string like "John Smith", the class should infer that the first name is John and last name is Smith. With TDD, before you even think of how that should be implemented, you'll write a test.

The Test Case

The first test just ensures that the full name returned is the same as the one given during construction. This is also the first look at a test case. The TestNameParser class is a child class of Test::Unit::TestCase. Each of the methods in TestNameParser beginning with test_ is a test to be run. When the script is run, each of the tests will be run and given a pass or fail.

One other thing to note is that, in order to prevent the repetition of creating a new object with the same parameters for every test, the setup method is used. If a setup method is defined, it will be run before every test. When it's run before every test, the tests don't have to worry about making any modifications to the objects created in setup, because setup will be run again before the next test. Similarly, the teardown method will be run after every test if it's defined.

The Initial Test Script

The initial test script looks like this. Save this script as tc_nameParser.rb. Whenever new modifications have been made, they'll be highlighted in bold. Be sure to make the same modifications in your script.

It's pretty straightforward. The setup method creates an object with the name "John Smith" for all the tests to use. The first test, with the name test_full_name asserts that the method full_name returns the correct string. The assert_equal is the basic assertion used in most tests, but there are a whole family of such assertions used by Test::Unit.

 require 'test/unit'
 class TestNameParser < Test::Unit::TestCase
   def setup
     @js = NameParser.new( "John Smith" )
   def test_full_name
     assert_equal( "John Smith", @js.full_name )

The next step is to run this test. Run this script as you would any other script. Don't worry that you haven't defined any "main" method or any way to run the methods in TestNameParser, this is taken care of for you by Test::Unit. But of course the tests will fail because we haven't defined a NameParser class yet.

 >ruby tc_nameParser.rb
 Loaded suite tc_nameParser
 Finished in 0.0 seconds.
   1) Error:
 NameError: uninitialized constant TestNameParser::NameParser
     tc_nameParser.rb:6:in `setup'
 1 tests, 0 assertions, 0 failures, 1 errors
  1. About.com
  2. Technology
  3. Ruby
  4. Advanced Ruby
  5. Test-Driven Development
  6. Test Driven Development in Ruby -- Writing Code

©2014 About.com. All rights reserved.