我正在尝试与OCMock一起编写XCTest(iOS7,XCode5)。
我有一个实现CLLocationManagerDelegate协议的类,并且有一个属性,它是CLLocationManager的一个实例。 (我将CLLocationManager的实例提供给我的初始化方法,以便我可以在运行时或测试时注入它。)
在测试委托类时,我创建了一个模拟CLLocationManager。
在测试中,我希望实现这样的目标:
[[[[mockLocationManager stub] classMethod] andReturnValue:kCLAuthorizationStatusDenied] authorizationStatus];
result = [delegateUnderTest doMethod];
//Do asserts on result etc etc
问题是,XCode抱怨我的代码。
test.m:79:68: Implicit conversion of 'int' to 'NSValue *' is disallowed with ARC
test.m:79:68: Incompatible integer to pointer conversion sending 'int' to parameter of type 'NSValue *'
kCLAuthorizationStatusDenied是我理解的int(在TypeDef中定义)。 所以,我不能用
[[[[mockLocationManager stub] classMethod] andReturn:kCLAuthorizationStatusDenied] authorizationStatus];
期望一个对象('andReturn'是'id')。
有什么想法吗?
答案 0 :(得分:2)
您需要在NSValue
实例中设置值,而不是传递原始值本身。例如:
[[[mockLocationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus];
上面使用了NSNumber
的Objective-C文字语法。另外,我省略了上面的classMethod
调用CLLocationManager
没有实例方法authorizationStatus
。
可以在OCMock website上找到更多支持:
如果方法返回基本类型,那么andReturnValue:必须与value参数一起使用。不可能直接传递原始类型。
这也是编译器错误告诉你的 - 你正在传递int
而不是NSValue
实例。