Test-driven development (TDD) is a series of small steps. It can be difficult to grasp until you see those steps demonstrated.

That’s why I made this screencast. It was sparked by a Stack Overflow question that said, “All the examples of unit testing I read about seem to be extremely simple and trivial.” The question asks how to write unit tests for a piece of sample code that uses NSUserDefaults.



Outline:

What to do with an external object (in this case, the NSUserDefaults singleton). Dependency injection to the rescue!

My basic setup: OCUnit + OCHamcrest + OCMockito (along with my test case template and test code snippets)

The 3 steps of “The TDD Waltz”

We TDD our way to the desired functionality

Show notes:

The Stack Overflow question. My original answer illustrates setter injection, as opposed to the (better) constructor injection of the video.

My Tools page, where you can get OCHamcrest, OCMockito, and my test case template

Clean Coders video series by Robert Martin

Bonus content: In addition to the screencast, I put together a bonus Git repository that shows you exactly how I’d TDD the problem, updated to my latest practices. You can access the bonus content here.

Question: What still remains puzzling or challenging about applying TDD to iOS development? Leave a comment below.

See also: How to Do UIViewController TDD