2015-10-19 49 views
0

我們生成CoralClient的OBJ-C代碼,這裏是從CoralCall類中的方法:如何嘲笑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對象在返回錯誤[電話:錯誤:]方法?我想單元測試重試邏輯,似乎沒有辦法做到這一點。

注:我可以很容易地嘲笑呼叫的成功,而不是失敗,是這樣的:

// 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); 

回答