Objective-C Singleton Pattern Updated For Testability

Objective-C Singleton Pattern Updated For Testability

Posted by on Jan 24, 2013 in BDD, iOS, iPad, iPhone, Mac OS X, Software Development, TDD | 11 Comments

At Two Bit Labs we do a fair amount of unit testing. In places where we use singletons we use a variation on the the Objective-C dispatch_once pattern of thread safe singleton creation. This variation supports resetting the singleton or replacing it with a mock object. That way in our unit tests we can do […]

Mocking singletons with OCMock

Posted by on Feb 28, 2011 in code | 20 Comments

Although Graham Lee might not approve, I occasionally use the singleton pattern, particularly for managing access to my data models. Singletons in Cocoa are accessed using the following convention: static UserModelManager *sharedManager = nil; +(UserModelManager *)sharedManager {     if (sharedManager == nil) {         sharedManager = [[super allocWithZone:NULL] init];     […]