Last week I created a new contact form for this page where you can ask me anything, and I’ll do my best to answer your question. From now on, I will post the most interesting questions and my answers here on my blog (and, of course, on my newsletter).

So, here is the very first question somebody asked me, and my answer. After reading it, try it yourself: Just press the big blue button at the top of this page, and ask a question…

I'd like to know more about TDD in Ruby, but most of the tutorials that I have seen have been focused on Java. Where do I start learning TDD in Ruby? LJ Nissen

Dear LJ,

thank you for your question. It was actually the first question that was submitted through my new contact form ;) And it’s already a tough one… At least for me. But I like that.

Alright. Before we start, let me say that I am by no means a Ruby expert. So take everything I say about Ruby with a grain of salt. But I don’t think the choice of Language is really that important here. Let me explain…

I know that quite a lot of TDD literature is written for Java. But there are people who do TDD in Ruby - after all, the whole “Is TDD Dead?” [1] discussion got started after David Heinemeier Hansson wrote an angry blog post [2]. So, can you use Java tutorials or a book like “Growing Object-Oriented Software, Guided by Tests” [3] even though you are using Ruby? I think so. But more on that later.

Before you start with those tutorials, you need to learn your test framework reasonably well, so you’re able to translate the examples from Java to your test framework. It probably also helps when you can read at least some Java code, but many tutorials describe the reasoning behind the code well enough so you should be able to implement it in Ruby.

There seem to be many different testing frameworks for Ruby [4], and I can’t really help you with choosing one. But the testing frameworks seem to have some good documentation (for example, here [5]).

Now you can start to learn the TDD flow. Don’t try anything fancy yet. Take a really simple example (like the “Prime Numbers Kata” [6] - The solution has less than 10 lines of code!) and write your first test. Get it to green. Refactor. Write the next test. Get it to green. Refactor.

Then you can try a fancier example. I like “Hangman” as an exercise: Write a hangman game and/or a bot that can play hangman. You can start either with the game or with the bot. The bot can be very simple (just guesses random letters), and you can evolve it to use more elaborate strategies (dictionary based, …).

Hangman is also great because you can do it “Inside-Out” or “Outside-In” [7], and now it is probably time to learn about the differences between those strategies. Also, try to find out how you can use Mocks [8] in Ruby, because you’ll probably need them for “Outside-In”.

When you have a good feeling for your testing framework, mocking framework and how you can refactor code in Ruby, you can start to do more tutorials or start reading “Growing Object-Oriented Software, Guided by Tests” [3]. Even though most of the examples you’ll find will be in Java or C#, by now you should be able to translate the learnings and reasonging behind the examples to Ruby.

You should pay special attention to the “refactoring” step in TDD. This is really important - If you don’t do it, your software design will suffer. Also make sure that you proceed in small, safe steps. “Taking Baby Steps” [9] and “Brutal Refactoring Game” [10] are two exercises that can help you here. You normally do those exercises in a group, at a conference or during a workshop. But you can practice them alone too.

Also, after the first few exercises and Katas, try to use TDD in a “real” (i.e. longer) project - Either a side project or at work. A Kata is nice to get started, but they are quite short lived. You do them, and when you are finished, you throw away the code. You will learn different things when you have to live with your code and your decisions for a longer time, like in a “real” project.

What questions do you still have? What will you try next?

Kind Regards, David

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]