我在方法中有此代码:
- (NetworkOperation *)runOperationWithPath:(NSString *)path
params:(NSDictionary *)params
httpMethod:(NSString *)httpMethod
completionHandler:(DictionaryCompletionHandler)completionHandler;
{
NetworkOperation *op = (NetworkOperation *)[self operationWithPath:path
params:[params mutableCopy]
httpMethod:httpMethod
ssl:YES];
[op addCompletionHandler:^(NetworkOperation *completedOperation) {
NSData *responseData = [completedOperation responseData];
NSError *error;
NSDictionary *returnDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
if(error == nil && !self.timedOut) {
completionHandler(returnDict, nil);
} else {
completionHandler(nil, error);
}
} errorHandler:^(NetworkOperation *completedOperation, NSError *error) {
//Manage error
}];
[self enqueueOperation:op];
return op;
}
我正在为此创建单元测试,并且发现了这种奇怪的行为。在单元测试中,我使用OHHTTPStubs对网络进行重新定位。单元测试的代码在这里
- (void)testThatTheRequestRunAndCallsTheCompletationBlock; {
//Here i have the setup of OHHTTPStubs to stub the responses.
__block BOOL called = NO;
[self.restClient runOperationWithPath:@"Rest/clientTest" params:nil httpMethod:@"POST" authenticationEngine:nil completionHandler:^(NSDictionary *result, NSError *error) {
called = YES;
}];
while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !blockFinished)
STAssertTrue(called, @"The completation block should be called");
}
如果我运行单个单元测试,则一切正常,如果我运行整个单元测试套件,则应用程序崩溃,调用compilationHandler为nil。
我不明白,因为在第一种情况(单次运行)中,嵌套块已正确复制,当我运行整个测试套件时,容器块未正确捕获嵌套块。
我尝试删除OHHTTPStubs并阻止等待代码,但没有任何变化。关于如何调试它的一些想法?
最佳答案
错别字:
- (void)testThatTheRequestRunAndCallsTheCompletationBlock; {
// ^