【问题标题】:How to mock CoralCall [call:error:] method to return error如何模拟 CoralCall [call:error:] 方法返回错误
【发布时间】: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


    【解决方案1】:

    看起来您想要做的事情在第 2.5 节的参考文档中有所描述(在传递引用参数中返回值):http://ocmock.org/reference/#stubing-methods

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多