0

Am just getting started with OCMock so bear with me - I have been looking through the documentation and loads of examples but am yet to find a definitive answer - is it possible to mock out an init call within a static method, for example:

+ (NSString *)addHeaderToRequest:(NSString *)request namespace:(NSString *)namespace
{        
    CTTeleInfo *netInfo = [[CTTeleInfo alloc] init];
    CTCarrier *carrier = [netInfo subscriberCell;
    NSString *mobileNetwork = [carrier carrierName];
    if ( mobileNetwork == nil )
        mobileNetwork = @"Unknown";

}

Is it possible to mock out the CTTeleInfo object without changing the code?

Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
ChrisH
  • 285
  • 3
  • 13

1 Answers1

1

This is not something OCMock can help you with. Even if it could mock class methods, which it can't yet, then you would still have to extract the creation of the netInfo object into a method the mock could overwrite.

If you can change the code, the obvious solution is this:

+ (NSString *)addHeaderToRequest:(NSString *)request namespace:(NSString *)namespace
{   
    [self addHeaderToRequest:request namespace:namespace carrier:[[CTTeleInfo alloc] init]];
}

+ (NSString *)addHeaderToRequest:(NSString *)request namespace:(NSString *)namespace carrier:(CTCarrier *)carrier
{ 
    NSString *mobileNetwork = [carrier carrierName];
    if ( mobileNetwork == nil )
        mobileNetwork = @"Unknown";
}
Erik Doernenburg
  • 2,933
  • 18
  • 21
  • 1
    Note that with current versions of OCMock it is now possible to stub class methods and methods creating objects. The old answer still outlines the preferred approach, though. – Erik Doernenburg Dec 22 '16 at 16:51