Objective-C TDD: How to Get Started

Shares

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.


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 Resources page, where you can get OCHamcrest, OCMockito, and my test case template
  • 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 practicesYou can access the bonus content here.

Access Bonus

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

See also: How to Do UIViewController TDD

About the Author Jon Reid

Jon is a coach and consultant on iOS Clean Code (Test Driven Development, unit testing, refactoring, design). He's been practicing TDD since 2001. You can learn more about his background, or see what services he can bring to your organization.

follow me on:

Leave a Comment:

65 comments
Add Your Reply