单元测试CLLocationManager的实现 [英] Unit Test CLLocationManager implementation
问题描述
我有一个名为 GeolocationManager
的类,该类处理 CLLocationManager
对象和 delegate
响应。
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是一个公共方法,随时可以进行存根
createLocationManager is a public method ready to be stubbed
- (CLLocationManager *)createLocationManager
{
return [[CLLocationManager alloc] init];
}
我要实现的目标是测试用户拒绝地理定位时, CLLocationManagerDelegate
方法 locationManager:didFailWithError:
在我的 GeolocationManager
上被调用对象。
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];
}
名称:NSInternalInconsistencyExceptionException文件:未知行:未知
原因:OCPartialMockObject [GeolocationManager]:未调用预期方法
:locationManager:OCPartialMockObject [CLLocationManager]
didFailWithError:
Name: NSInternalInconsistencyException File: Unknown Line: Unknown Reason: OCPartialMockObject[GeolocationManager]: expected method was not invoked: locationManager:OCPartialMockObject[CLLocationManager] didFailWithError:
推荐答案
您是否尝试过使用 OCMOCK_VALUE(kCLAuthorizationStatusDenied)
作为returnValue?
Have you tried using OCMOCK_VALUE(kCLAuthorizationStatusDenied)
for the returnValue?
这篇关于单元测试CLLocationManager的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!