Wednesday, November 12, 2008

Get over yourself and write that test

I've had to tell myself this a bit recently...and it wasn't the medium you'd expect.

I'd been working on a particularly tricky Sudoku puzzle...you know the kind where you've got lots of squares completely solved, and yet some are mostly blank? No matter what I did, I couldn't find a damn 7,8, or 9 to set off a finishing chain of selections. I also have this stubbornness that forces me to try and solve the hard puzzles without using notes. I almost always can, and yet this particular puzzle was besting me.

Its ok, I just have to try harder, right? Focus more, go over the various possibilities, what could this being here mean, and so on. Another 20 minutes pass and no progress. With a sigh I finally gave up, and started sketching out all of the possibilities. Within 3 seconds of doing so, I saw an obvious selection I had missed. This set off another, and another. 2 minutes later I was done.

Part of me felt like I "cheated"--any great Sudoku solver wouldn't need such help, right? At the same time, there's a point where the challenge of solving the puzzle degrades into frustration and really a waste of my time, and in this case I had long overshot that mark.

Feel familiar yet?

I couldn't help smiling as I thought about this on the metro in this morning. It reminded me of every time I'd sit down to write some code, get it wrong, redo some things, get it right...and then find out I really didn't...and repeat this process until I'd finally write a few tests and voila, be done with it.

I remember back in college hearing these epic stories of masters who could code in C, top to bottom, and at the end magically compile things without error. I think of people I know who can jump into a presentation, test, interview, or module, and just get what they need done without a moment's hesitation or need of 'backup' or notes.

I used to be one of those people...but I think lots of us were, back in 5th grade, just excited to learn the next set of formulas or geometric equations. I'm not anymore, and I'm ok with that. There's a certain peace of mind, a certain relaxation I feel, when I have code that's spec'ed, tested, and banged on to the point where I own it, and not the reverse.

Something I think people don't say enough, even with the tons of discussions about tests: even if your tests suck, even if they mock too much, are too brittle, and perhaps miss some important cases, the knowledge of the code that you receive while writing those tests is extremely valuable. The investment in your own ability to write those tests is extremely valuable. Testing really is like working out, and I've slipped as of late (in both areas), and can feel it.

I made the decision recently to change both of these things, and just like getting back into the gym has given me more energy and I just feel better, getting back into writing some tests has reminded me just why we should do it in the first place.

I've been flexing this rebirth with a simple merb 1.0 service example app located at http://github.com/jackdempsey/merb-service-example/tree/master and you'll see its using the new request helper. I'd talk more about that, but wycats has done so already, so I'll just say: you really, really want to test this way as opposed to mocking everything out. It allows for a very fluid and flexible inner workings of your controller, while verifying things come back as they should.

PS: I even TDD'ed this post via a friend checking it. As you'd expect, I was shown several 'bugs' and issues. Thanks again Doug

2 comments:

The Grovemeister said...

Great post Jack. I recently watched wycats' presentation from merbcamp about testing and really liked the bit "if your app works and your tests fail then your tests are broken"(sic). For this reason, I'm trying to move away from mocking apart from for external api calls.

Jack Dempsey said...

Exactly. How many times did you have to rewrite your tests because you were changing some little internal method name, even with the outcome being the same? Definitely broken.