Search code examples
iphoneiosobjective-c-blocksocunitsentestingkit

STFail in block


When running tests using SenTestingKit, they don't fail correctly inside a block, e.g.

operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                            success:^(NSURLRequest *req, NSHTTPURLResponse *response, id jsonObject){
                                                                STFail(@"This does not fail");
                                                            }
                                                            failure:^(NSURLRequest *req, NSHTTPURLResponse *response, NSError *error, id jsonObject){
                                                                STFail(@"Neither does this");
                                                            }];
[operation start];
STFail(@"But this fails fine");

What am I missing?


Solution

  • The trouble is that you have an asynchronous call. Which means that the it returns immediately and your last STFail is hit.

    There are solutions to be had. How to unit test asynchronous APIs?