February 26, 2016 Update: Read to the end for Git repo bonus!
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.
- 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
- The Stack Overflow question. My original answer illustrates setter injection, as opposed to the (better) constructor injection of the video.
- My Resources page, where you can get OCHamcrest, OCMockito, and my test case template
- AppCode, an alternative IDE with much more automated refactoring than Xcode
- Clean Coders video series by “Uncle Bob” Martin
Update: 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.