This is the piece of code i am testing. When running it (especially under automated circumstances using tools such as Jenkins) i will always get the WiFi response, which leaves the other part untested.
- Code: Select all
- (NSString *)connectionType {
Reachability *reach = [Reachability reachabilityForLocalWiFi];
if (reach.currentReachabilityStatus == ReachableViaWiFi) {
return @"WiFi";
} else {
reach = [Reachability reachabilityForInternetConnection];
if (reach.currentReachabilityStatus == ReachableViaWWAN) {
return @"MobileCarrier";
}
}
return @"Undefined";
}
I then figured i should try mocking the Reachability class and return what ever value i wished for the method used, like this:
- Code: Select all
id mock = [OCMockObject mockForClass:[Reachability class]];
[[[mock stub] andCall:@selector(ocMockReturn) onObject:self] currentReachabilityStatus];
...
- (NetworkStatus)ocMockReturn {
return ReachableViaWWAN;
}
It seems however that this is never getting called. And from what i've read i might have to use method-swizzling instead, but then i would have to mock a certain object which in this case is not visible to me where i test the code. Is there anyway around all this so that i can also mock and test code such as this using this framework?
