Unit Test CLLocationManager implementation

瘦欲@ 提交于 2019-12-25 04:53:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!