Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CLLocationManagerDelegate对用户的模拟拒绝_Ios_Unit Testing_Ocmock - Fatal编程技术网

Ios CLLocationManagerDelegate对用户的模拟拒绝

Ios CLLocationManagerDelegate对用户的模拟拒绝,ios,unit-testing,ocmock,Ios,Unit Testing,Ocmock,--最终编辑: 更准确的问题 我试图模拟CLLocationManagerDelegate来处理用户拒绝地理定位的情况 id locationManager = [OCMockObject niceMockForClass:[CLLocationManager class]]; [[[locationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus]; id delegateTar

--最终编辑: 更准确的问题

我试图模拟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]错误:

如何确保调用了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];
我明白了:

由于未捕获的异常“nsInternalInconsistenceException”而终止应用程序,原因:“OCMockObject[CLLocationManagerDelegate]:调用了意外的方法:locationManager:didChangeAuthorizationStatus:2 应为:locationManager:didFailWithError:'

因此,我认为我得到了它,并在我的delegateTarget期望DidFailwerRor之前添加了下面的代码

[[delegateTarget expect] locationManager:trueLocationManager didChangeAuthorizationStatus:2]; // kCLAuthorizationStatusDenied = 2
结果是:

名称:NSInternalInconsistencyException 档案:未知 线路:未知 原因:OCMockObject[CLLocationManagerDelegate]:未调用预期的2个方法: locationManager:didChangeAuthorizationStatus:2 位置经理:错误:

真的不知道我做错了什么

编辑2:给伊莱娅·克里斯蒂安

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];
我明白了:

由于未捕获的异常“nsInternalInconsistenceException”而终止应用程序,原因:“OCMockObject[CLLocationManagerDelegate]:调用了意外的方法:locationManager:didChangeAuthorizationStatus:2 应为:locationManager:OCPartialMockObject[CLLocationManager]didChangeAuthorizationStatus:2 应为:locationManager:OCPartialMockObject[CLLocationManager]错误:'

看起来委托回调中使用的locationManager是trueLocationManager,而不是locationManager(partialMock)

--最终编辑: 更准确的问题

您已经为您的LocationManager创建了一个“不错的模拟”。这意味着您对
setDelegate:
startUpdatingLocation
的调用将被静默忽略。您可以将其切换为部分模拟,但您只是在测试LocationManager吗?您应该只需要测试location manager委托的实现——您可以直接调用这些委托方法。

您已经为LocationManager创建了一个“很好的模拟”。这意味着您对
setDelegate:
startUpdatingLocation
的调用将被静默忽略。您可以将其切换为部分模拟,但您只是在测试LocationManager吗?您应该只需要测试location manager委托的实现——您可以直接调用这些委托方法。

您有这样一行:

[locationManager setDelegate:delegateTarget];
这意味着当调用
locationManager:didFailWithError:
时,为
CLLocationManager
参数传递的对象将是名为
locationManager
的OCMock对象,而不是
trueLocationManager

您的期望:

[[delegateTarget expect] locationManager:trueLocationManager didFailWithError:[OCMArg any]];
因此,您期望一个调用传递
trueLocationManager
,而不是传递部分模拟。尝试期望通过部分模拟

OCMock部分mock,如
locationManager
,只是真实对象上的代理包装器,但它们的类型不同(这就是为什么使用
id
而不是真实类型)

这种魔力之所以有效,是因为Objective-C通过消息传递处理调用的方式。如果你读了一点关于,然后你看看,你会真正理解一些后台的东西是如何工作的

编辑:

就我个人而言,我不知道你真正想测试什么。苹果框架?没有一个实体是您的类/协议。你在测试什么

我猜您有一个实现CLLocationManagerDelegate协议的ViewController。嘲笑那门课。如果您的位置管理器是一个属性,则将其存根以返回您的假位置管理器(以便vc在内部使用假位置管理器)。或者将伪locationManager.delegate设置为您的ViewController。启动假位置管理器

期望调用ViewController委托方法。如果您的位置经理是单身汉,则使用假/存根。

您有以下行:

[locationManager setDelegate:delegateTarget];
这意味着当调用
locationManager:didFailWithError:
时,为
CLLocationManager
参数传递的对象将是名为
locationManager
的OCMock对象,而不是
trueLocationManager

您的期望:

[[delegateTarget expect] locationManager:trueLocationManager didFailWithError:[OCMArg any]];
因此,您期望一个调用传递
trueLocationManager
,而不是传递部分模拟。尝试期望通过部分模拟

OCMock部分mock,如
locationManager
,只是真实对象上的代理包装器,但它们的类型不同(这就是为什么使用
id
而不是真实类型)

这种魔力之所以有效,是因为Objective-C通过消息传递处理调用的方式。如果你读了一点关于,然后你看看,你会真正理解一些后台的东西是如何工作的

编辑:

就我个人而言,我不知道你真正想测试什么。苹果框架?没有一个实体是您的类/协议。你在测试什么

我猜您有一个实现CLLocationManagerDelegate协议的ViewController。嘲笑那门课。如果您的位置管理器是一个属性,则将其存根以返回您的假位置管理器(以便vc在内部使用假位置管理器)。或者将伪locationManager.delegate设置为您的ViewController。启动假位置管理器

期望调用ViewController委托方法。如果你的位置经理