问题
I have a class named "GeolocationManager
" that handles a CLLocationManager
object and delegate
responses.
- (void)getGeolocationForClient:(id<GeolocationManagerDelegate>)client
{
_delegate = client;
_locationManager = [self createLocationManager];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// do some stuff
}
createLocationManager is a public method ready to be stubbed
- (CLLocationManager *)createLocationManager
{
return [[CLLocationManager alloc] init];
}
What I want to achieve is to test that when user denies being geolocated the CLLocationManagerDelegate
method locationManager:didFailWithError:
is being called on my GeolocationManager
object.
My test method below does not seem to be working
- (void)testOnUserDenial
{
GeolocationManager *geolocationManager = [[GeolocationManager alloc] init];
id geolocationManagerTest = [OCMockObject partialMockForObject:geolocationManager];
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
id locationManagerTest = [OCMockObject partialMockForObject:locationManager];
[[[locationManagerTest stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus];
[[[geolocationManagerTest stub] andReturn:locationManagerTest] createLocationManager];
[[geolocationManagerTest expect] locationManager:[OCMArg any] didFailWithError:[OCMArg any]];
[geolocationManagerTest getGeolocationForClient:[OCMArg any]];
[geolocationManagerTest verify];
}
Name: NSInternalInconsistencyException File: Unknown Line: Unknown Reason: OCPartialMockObject[GeolocationManager]: expected method was not invoked: locationManager:OCPartialMockObject[CLLocationManager] didFailWithError:
回答1:
Have you tried using OCMOCK_VALUE(kCLAuthorizationStatusDenied)
for the returnValue?
来源:https://stackoverflow.com/questions/20376000/unit-test-cllocationmanager-implementation