Over the past month and half I’ve been working hard on my TDD skills. Having come from a learn-as-you-go web development background (as a lot of web developers have) I’ve found the transition to a test-first workflow to be a real challenge. Luckily, I work with a great group of Rubyists and Madison, Wisconsin has a great Ruby community.
First step: RTFM (or RTFB, in this case) RSpec is central to my everyday workflow. I work in a Ruby shop and have the luxury of working with people who have already mastered this stuff. While this is great, it also means that most of my experience with RSpec up until now has been with maintaining and extending a test suite, not starting from scratch and driving out features.
To begin my study of RSpec, I picked up the RSpec Book (David Chelimsky), which is really more of a TDD/BDD book than it is purely RSpec. It does a fantastic job of covering the BDD development cycle and it’s more of a conicidence that the samples are in Ruby.
The cycle is described as follows:
Focus on one scenario (Cucumber)
Red: Write a failing step definition (Cucumber)
Red: Write a failing example (RSpec)
Green: Write code to make the example in #3 pass (RSpec)
Refactor: Refactor your code, ensure your tests continue to pass (RSpec)
Return to Step #2 and continue to through your step definitions until all are passing
Refactor: Refactor your code, keeping both RSpec and Cucumber test suites passing
The RSpec Book is worth the money on the merit of its coverage of the BDD cycle alone. I also appreciated that the author, David Chelimsky, chose to use a command-line application as the central example rather than a web app. While Rails is a great framework, I’m discovering that if you’re not careful to ground yourself in Ruby fundamentals, you can easily lose your way in the conveniences of the framework and write a lot of bad code. The command-line app gets back to basics and helps liberate the user from having to worry about browsers, databases and servers for awhile and focus on the code itself.
More complex example: Conway’s Game of Life The next step in my process was to apply it to test-driving Conway’s Game of Life. Conway’s Game of Life is a classic programming exercise used to demonstrate object oriented programming techniques. As I began working on the problem, I was stumped. As I mentioned earlier, most of my experience is in maintaining a mature test suite. This means that much of the infrastructure and helpers have been defined. This was a brand new endeavor: new project, empty test suite and an unfamiliar topic. It was the programming equivalent of writer’s block.
To get unstuck, I did some searching to see if there were any solutions to this problem that I could study. As luck would have it, there is a great screen cast of Ryan Bigg tackling Conway’s Game of Life using RSpec and Ruby. I watched the video a couple times and was able to implement all four rules after getting a feel for how a master approaches TDD.
Global Day of Code Retreat While it did feel good to get Game of Life functioning, I felt like I had cheated by watching someone else implement the first rule. As luck would have it, I was able to get a ton more practice at the Global Day of Code Retreat.
The Global Day of Code Retreat, held on December 7th, was a collection of local events around the world where programmers worked together along with a facilitator to practice BDD on (you guessed it) Conway’s Game of Life. After each 45 minute session, you trash your code. It’s not about the result, but about putting the principles of BDD into practice. After hacking at the Game of Life from 9am to about 4pm, I learned a valuable lesson: BDD is about practice.
That’s all there is to it. I’m not going to get it all at once. It’s an approach and an attitude, not a silver bullet. After studying RSpec for a month and a half, I’ve learned that I’ll be studying RSpec, or more specifically BDD, for the rest of my career. And that’s fine by me.
About the 1up Project The 1up Project is the brain-child of Joe Nelson. It is a self-directed program of personal-improvement where the participants choose specific topics, study them over a discrete period of time while applying the knowledge gained to a specific project, presentation or blog post.