块导入嵌套块失败

时间:2013-10-10 15:18:59

标签: ios objective-c unit-testing block objective-c-blocks

我在方法中有这个代码:

- (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;

}

我正在为它创建单元测试,我发现了这个奇怪的行为准则。在单元测试中,我使用OHHTTPStub来存根网络。单元测试的代码在这里

- (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为零。 我不明白,因为在第一种情况下(单次运行)嵌套块被正确复制,当我运行整个测试套件时,容器块没有正确捕获嵌套块。

我尝试删除OHHTTPStubs并阻止等待代码,但没有任何变化。关于如何调试它的一些想法?

1 个答案:

答案 0 :(得分:1)

错字:

- (void)testThatTheRequestRunAndCallsTheCompletationBlock; {
//                                                       ^