Search code examples
objective-cunit-testingsingletonstatic-initializer

Weird singleton initialization in static function initialize during unit tests


I have a following code in my singleton class

static MySingleton *gManager;
+(void)initialize
{
   if(self == [MySingleton class])
   {
       gManager = [[MySingleton alloc] initWithServices:[[MyServices alloc] init]];
   }
}
+(MySingleton *)sharedInstance
{
   return (gManager);
}

Unfortunately, during the unit tests I see that gManager is an instance of type SenTestCaseRun. I cant seem to figure out why? So a call like [[MySingleton sharedInstance] myFunction];

results in an error that myFunction is an unknown selector although it exists in the MySingleton class.

It is of type SenTestCaseRun because I checked using NSStringFromClass function.

Any pointers? Already banged my head for 3-4 hours on this :(.


Solution

  • Put a breakpoint in +initialize to make sure this variable is set correctly. If that doesn't explain it, use a watchpoint on it to see who's modifying it.