Yesterday, I was having a discussion with a young, bright, aspiring developer about code, and the subject of tests came up. In the Ruby community these days, the assumption is that everyone tests their apps, so our conversation evolved to the actual testing practice. I appreciate the many approaches to testing, so I started quizzing him about his process.
The conversation moved along nicely until I asked him, “Say you have a new Rails project. What is the first test that you write?” He described that he would use the Rails generators to create a model and then start describing the behaviors for his first model. While not trying to sound rude, I probed if that was really the first test he should write? By generating a model and describing its behaviors, he was subconsciously making a few serious assertions about his application. How do you know you’ll need a relational database? Why would you pick any model over any other? How does that model even fit inside of your applications ecosystem?
Now, with a genuine fear of sounding overly pedantic, I’ve had great internal debates about The First Test. This single test sets the tone for your initial development session. To avoid wasting too much time, what test do you write first?
Today, I’ll try to explain how I write the first test. Maybe, someone can find great errors in my ways, and enlighten me to a better way. Maybe I’ll learn that I’ve been wasting my time, and should move on to more important problems.
My process is pretty simple. In my opinion, the first test is not a unit test. Without a test, those units have no reasons to exist. I start off by writing a higher level functional test. In my parlance, a functional test does not necessarily equate to a Rails functional test. A functional test simply verifies the behavior between objects in a system. Also, a unit test can only describe the behavior of a single unit in isolation in one or more circumstances. So, what’s the first test that I write? My first test describes the simplest assumption of my system. If I was writing a blog, describing the process of creating a new post might be my first task. The first test would most likely have some sort of mock implementation of a post that would naively say it saved itself when asked. Next, after I assured that my process for creating the post was correct, I would then move to implement some type of object to encapsulate a Post.
I believe this method allows me to describe the system without concerning myself with details of how it works. Is this crazy? What say you?