Luca Bartoletti
Luca Bartoletti

Reputation: 2447

A block fail importing a nested block

I have this code in a method:

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

}

I'm creating unit tests for it and i have found this strange beahviour. In the unit test i'm using OHHTTPStubs to stub the network resposes. The code of the unit test is here

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

}

If i run the single unit test all works well, if i run the whole suite of unit tests the app crash calling the completationHandler that is nil. I don't understand because in the first case (single run) the nested block is correctly copied, when i run the whole tests suite the nested block aren't correctly captured by the container block.

I tried removing the OHHTTPStubs and block wait codes but nothing changes. Some idea on how i can debug it?

Upvotes: 1

Views: 118

Answers (1)

trojanfoe
trojanfoe

Reputation: 122458

Typo:

- (void)testThatTheRequestRunAndCallsTheCompletationBlock; {
//                                                       ^

Upvotes: 1

Related Questions