单元测试CLLocationManager实现

时间:2013-12-04 12:55:42

标签: ios objective-c unit-testing cllocationmanager ocmock

我有一个名为“GeolocationManager”的班级,可以处理CLLocationManager个对象和delegate个回复。

- (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是一个准备好被存根的公共方法

- (CLLocationManager *)createLocationManager
{
    return [[CLLocationManager alloc] init];
}

我想要实现的是测试当用户拒绝进行地理定位时,我的CLLocationManagerDelegate对象上正在调用locationManager:didFailWithError:方法GeolocationManager

我的测试方法似乎不起作用

- (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];
}
  

名称:NSInternalInconsistencyException文件:未知行:未知     原因:OCPartialMockObject [GeolocationManager]:期望的方法是   未调用:locationManager:OCPartialMockObject [CLLocationManager]   didFailWithError:

1 个答案:

答案 0 :(得分:0)

您是否尝试将OCMOCK_VALUE(kCLAuthorizationStatusDenied)用于returnValue?