【发布时间】:2015-10-19 14:04:19
【问题描述】:
我们从 CoralClient 生成 Obj-C 代码,这里有一个来自 CoralCall 类的方法:
- (id)call:(CoralModel *)request error:(NSError * __autoreleasing *)error;
这基本上是我使用 CoralCall 的代码:
Request *request = ...;
// Make the Coral call
NSError *error = nil;
int count = 0;
do {
// There's no result, because this API won't return anything
[coralCall call:request error:&error];
++count;
} while (error != nil && count < MAX_RETRY_COUNT);
completionHandler(error);
如何使用 OCMock 模拟coralCall 对象以在 [call:error:] 方法中返回错误?我想对重试逻辑进行单元测试,似乎没有办法实现这一点。
注意:我可以轻松地模拟调用成功,但不能模拟失败,如下所示:
// Doesn't matter what the returned value is, so use nil for simplicity
OCMStub([self.coralCall call:[OCMArg any] error:(NSError * __autoreleasing *)[OCMArg anyPointer]]).andReturn(nil);
更新:现在通过 Erik 的回答解决了这个问题。如果有人需要答案,这里是如何模拟错误:
NSError *error = OCMClassMock([NSError class]);
OCMStub([self.coralCall call:[OCMArg any] error:[OCMArg setTo:error]]).andReturn(nil);
【问题讨论】:
标签: ios unit-testing ocmock