Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCMock Class Method Not Working

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.

like image 357
Abdalrahman Shatou Avatar asked Jun 21 '26 14:06

Abdalrahman Shatou


1 Answers

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.

like image 96
Michał Ciuba Avatar answered Jun 24 '26 06:06

Michał Ciuba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!