3

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.

Abdalrahman Shatou
  • 4,550
  • 6
  • 50
  • 79
  • Does using the provided `OCMOCK_VALUE()` macro yield better results? The macro creates an `NSValue`, which is different from the `NSNumber` you are passing – Paul.s Mar 12 '14 at 15:44
  • Same results, no difference. – Abdalrahman Shatou Mar 12 '14 at 15:46
  • This should work. In the code you posted it's not clear how the controllers are created. Is it possible that they call the `isIO6_earlier` method when they are created, before the stub is set up? – Erik Doernenburg Mar 13 '14 at 19:11
  • @ErikDoernenburg I updated the code to clear it out. Still not working. The viewDidLoad method is the one detecting the iOS and changing the color according to it. – Abdalrahman Shatou Mar 16 '14 at 09:21
  • With the code you posted: [[iOSDetectorMock stub] andReturnValue:@(NO)] isIOS6_Earlier] the mock will always return NO. Or are you using [.. andReturnValue:@YES] in your actual test code? – Michał Ciuba Mar 16 '14 at 13:17
  • @MichałCiuba Sorry, a copy paste mistake. Fixed it now. – Abdalrahman Shatou Mar 16 '14 at 15:39
  • Maybe try to make it more explicit: [[[[iOSDetectorMock stub] classMethod] andReturnValue:@YES] isIOS6_Earlier]. As described on http://ocmock.org/features/, it can be helpful in some cases. – Michał Ciuba Mar 16 '14 at 17:17
  • Are you sure the mock is being called? If you use an "andDo" block and set a break point in the block, do you see it stop there? Is it possible that the logic in your VC is actually broken? – Ben Flynn Mar 17 '14 at 17:15
  • @MichałCiuba yes, it worked as you said. Please, post it as answer to accept it. – Abdalrahman Shatou Mar 17 '14 at 20:33

1 Answers1

3

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.

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59