【发布时间】:2023-04-05 13:30:01
【问题描述】:
-- 最终编辑: 更准确的问题Unit Test CLLocationManager implementation
我正在尝试模拟 CLLocationManagerDelegate 来处理用户否认被地理定位的问题
id locationManager = [OCMockObject niceMockForClass:[CLLocationManager class]];
[[[locationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus];
id delegateTarget = [OCMockObject niceMockForProtocol:@protocol(CLLocationManagerDelegate)];
[[delegateTarget expect] locationManager:locationManager didFailWithError:[OCMArg any]];
[locationManager setDelegate:delegateTarget];
[locationManager startUpdatingLocation];
[delegateTarget verify];
但是这段代码不起作用,我一直收到这个错误:
名称:NSInternalInconsistencyException 文件:未知 线路:未知 原因:OCMockObject[CLLocationManagerDelegate]:未调用预期方法:locationManager:OCMockObject[CLLocationManager] didFailWithError:
如何确定 CLLocationManagerDelegate 方法 locationManager:didFailWithError: 被调用?
编辑: 如果我对 CLLocationManager 实例使用 partialMock 而不是 niceMock:
CLLocationManager *trueLocationManager = [[CLLocationManager alloc] init];
id locationManager = [OCMockObject partialMockForObject:trueLocationManager];
[[[locationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus];
id delegateTarget = [OCMockObject mockForProtocol:@protocol(CLLocationManagerDelegate)];
[[delegateTarget expect] locationManager:trueLocationManager didFailWithError:[OCMArg any]];
[locationManager setDelegate:delegateTarget];
[locationManager startUpdatingLocation];
[delegateTarget verify];
我明白了:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“OCMockObject[CLLocationManagerDelegate]:调用了意外的方法:locationManager:didChangeAuthorizationStatus:2 预期:locationManager:didFailWithError:'
所以我以为我明白了,并在我的 delegateTarget 对 didFailWithError 的期望之前添加了下面的代码
[[delegateTarget expect] locationManager:trueLocationManager didChangeAuthorizationStatus:2]; // kCLAuthorizationStatusDenied = 2
结果是:
名称:NSInternalInconsistencyException 文件:未知 线路:未知 原因:OCMockObject[CLLocationManagerDelegate]:未调用 2 个预期方法: 位置管理器:didChangeAuthorizationStatus:2 locationManager: didFailWithError:
真的不知道自己做错了什么……
Edit2:为 Ilea Cristian
CLLocationManager *trueLocationManager = [[CLLocationManager alloc] init];
id locationManager = [OCMockObject partialMockForObject:trueLocationManager];
[[[locationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus];
id delegateTarget = [OCMockObject mockForProtocol:@protocol(CLLocationManagerDelegate)];
[[delegateTarget expect] locationManager:locationManager didChangeAuthorizationStatus:2]; // kCLAuthorizationStatusDenied = 2
[[delegateTarget expect] locationManager:locationManager didFailWithError:[OCMArg any]];
[locationManager setDelegate:delegateTarget];
[locationManager startUpdatingLocation];
[delegateTarget verify];
我知道了:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“OCMockObject[CLLocationManagerDelegate]:调用了意外的方法:locationManager:didChangeAuthorizationStatus:2 预期:locationManager:OCPartialMockObject[CLLocationManager] didChangeAuthorizationStatus:2 预期:locationManager:OCPartialMockObject[CLLocationManager] didFailWithError:'
看起来委托回调中使用的 locationManager 是 trueLocationManager 而不是 locationManager (partialMock)
-- 最终编辑: 更准确的问题Unit Test CLLocationManager implementation
【问题讨论】:
标签: ios unit-testing ocmock