Unit Testing Prototype Code

I’ve often been told that unit testing things like views, that are very visual, doesn’t make sense. My typical response is “Ok, show me an example of code that shouldn’t be unit tested”. I’m honestly open to the idea that there are situations where unit testing isn’t a good thing, but I haven’t come across very many. So here I’m going to suggest a situation where this may be the case.

I’m currently extending the What’s My Speed code that I developed for the lynda.com unit testing course. The original code had just a MapView with a couple large text views to display time and speed.

I decided to spiff things up a bit, and put either some LED bar graph gauges, or perhaps round, automotive style meters to display temperature, fuel level, etc. Looking around for some open source custom views for these, I came across F3BarGauge. This seems to do a good job of displaying the LED bars. So now I need to add labels for a title (eg. “Fuel”), and level labels (eg. “Full”, “1/2”, etc). We’ll want to wrap all this into a custom view.

Now, at this point, I don’t really know what I want this thing to look like. If I was working with a designer, I’d have the designer figure out what it should look like, create me an Illustrator or Photoshop asset, and go to work implementing the code to display it. But I’m not working with a designer, so I’m going to use code to do all this.

What I’m going to do is:

  1. Using the storyboard, drag a bunch of labels and such onto it.
  2. Hook things up just enough to actually see the control in operation.
  3. Mess with the fonts, sizes, and colors until I get something I like.

Once this is done, I fully expect to have a real mess code-wise. But I’ll know then what I want it to look like. I refer to this as prototyping.

Aha! This may be an example where TDD doesn’t make sense. I don’t know what I want the code to do yet. Now, strictly speaking, I could write unit tests before I write each piece of code. But since I know ahead of time that I’m going to be making lots of changes and adjustments, I think it best to wait until the prototype is done, then start all over using TDD.

So that’s what I’m doing. I prototyped using mostly IB, then once I had the design where I liked it, I started rewriting everything using TDD. But the prototype itself contained no unit tests (blush).

 

Leave a Reply