Rails Girls Next

February 19, 2014

Exercises

Tips and Tricks

TDD Exercise

Created by Greg McIntyre, @gregmcintyre

This exercise was run for me by a great guy named Steve Hardisty when we both worked at REA Pty Ltd. It is intended as an exercise to teach you what we're talking about when we say Test Driven Development (TDD). It also involves pair (or in our case group) programming.

Overview

You will be writing a Roman Numeral converter using TDD. The converter will take a Roman Numeral (i.e. "I", "IV", or "XX") and convert it into a number (i.e. 1, 4, 200). Here's a quick reminder on how Roman numerals work.

Step 0 - Git

Pick someone to create your GitHub repo. Then share access to the repo with everyone in the group.

Step 1 - Initial Code

A mentor will work with you for the first step:

def roman(n)
  return "?"
end

require "minitest/spec"
require "minitest/autorun"
require "minitest/pride"

describe "roman" do
  it "converts the number 1 to the string I" do
    roman(1).must_equal "I"
  end
end

Run the test using Ctrl-B in Sublime Text or typing the following at the prompt:

ruby roman.rb

Outputs:

roman#test_0001_converts the number 1 to the string I [tdd1.rb:11]:
Expected: "I"
  Actual: "?"

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

Your code is now red - one or more of the tests fail. Commit these changes, and push them up to the repo. You should commit and push after each step.

Step 2 - Write code

Pick someone in your group to drive (you'll all do it, so don't be shy!). Pull the code from the repo to make sure you have all the changes made in the previous step. You should pull before each step.

The new driver will try to make the test pass. They may do this however they see fit, with feedback and hints from the other group members. It's fine if the change is just an extra if statement. In fact, that's a great idea - it encourages the person writing the test to try harder to write good tests.

Your code is now green - all of your tests pass. Commit and push again.

Step 3 - Write test

The next person is now the driver, and the other two are observers. Pull again.

The new driver should write another test that will fail. You can write any test you like, so long as it moves you towards the goal of converting Roman Numerals. In general your tests will have a logical progression, but you'll find you might need to go back and add tests - that's completely fine! The observers should continue to give suggestions and hints. (But they should NOT take over the keyboard, they're observing!)

Your code is now red again, time to commit and push.

Step 4 - Rinse and Repeat!

Now you have the idea, you can keep repeating steps 2 and 3, making sure to continue switching the driver each time.

Whenever you feel like some of the code needs to be refactored (cleaned up and made easier to read), stop and work through the refactor. You should only refactor when your code is green, you should work through the refactor as a group, and your code should be green by the end of the refactor. Make sure you commit after a refactor.

Final Git

Don't forget to fork the repo when you're done!