Not sure what test to write next? Create a TODO list with pending specs
You know you can get started doing TDD by asking questions… but what if you have lots of questions? Where do you start? What do you do when asking one question makes you think of three other questions? And how do you turn these questions into tests that you can use to help you create software?
Here’s something simple you can do: create a TODO list! But not just any TODO list… you can create your TODO list directly in your tests, so that you don’t forget about them. Just create a TODO comment for every question you have, and turn the question into a statement:
Or, if you’re using RSpec, you can use pending examples:
And when you run the tests, RSpec will tell you the tests are pending, and print your TODO list in a readable form:
Just write the test descriptions. Don’t write any code inside the tests (yet). That part can come after, once you’ve clarified your questions. Right now, you just want to answer the question, “What should the software do?” And the best way to do that is to break it down into even smaller questions.
Take your time on this. Keep reworking your pending specs until they make sense to you as a group. Keep reworking them until you have a good understanding of what you want the software to do.
And then when you’re ready, start the TDD process – one test at a time. Turn your first pending test into a failing test. Write the implementation code to make the test pass. Refactor :) And then move on to the next one.
Here’s a little story for you… years ago, I was pairing with David Chelimsky (the main guy behind RSpec for a long time). We were faced with this exact question – what tests do we need to write? I opened a new file to write down our TODO list. David looked at me and said, “Dude… pending specs.” He had just added them to RSpec earlier that week, and I thought he just wanted an excuse to use them. They turned out to be incredibly useful, and I still use this approach in my work today.