Question

I am trying to mock a class method that identifies if the iOS is iOS 6 (or earlier) or iOS 7 (or later). Here is the testing code:

id iOSDetectorMock = [OCMockObject mockForClass:[UTiOSVersionDetector class]];
[[[iOSDetectorMock stub] andReturnValue:@(YES)] isIOS6_Earlier];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController];
[self.homeViewController loadView];
[self.homeViewController viewDidLoad];
XCTAssertTrue([self.homeViewController.navigationController.navigationBar.tintColor isEqual:Color_Dark_Green], @"Navigation bar tint color is not correct");
[iOSDetectorMock stopMocking];
navController = nil;

However, the method isIOS6_Earlier keeps on returning NO neglecting the mock I used. The original code of UTiOSVersionDetector is below:

+ (BOOL) isIOS6_Earlier {

if ([[[UIDevice currentDevice] systemVersion] intValue] <= 6) {
    return YES;
}

return NO;

}

Edit: I am testing on Xcode 5.1, yet it exists on Xcode 5.0.2 too.

Was it helpful?

Solution

Maybe try to make it more explicit:

[[[[iOSDetectorMock stub] classMethod] andReturnValue:@YES] isIOS6_Earlier];

As described on ocmock.org/features, it can be helpful in some cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top