I found this thread as a reference: How do I wait for an asynchronously dispatched block to finish? and am using that code (signaling the semaphore inside the block) to wait on my thread to finish before finishing the test.
However my semaphore never signals that its done... so I stepped through the code and found that the code inside this little snippet never runs:
dispatch_async(dispatch_get_main_queue(), ^{
[self removeDownloadSpinner];
block(callback);
});
Does dispatch_get_main_queue() not fire when I am unit testing? If not, how do you unit test async blocks?
EDIT (Here is a little more code): Basically I have a "ServerRequest" class that contains all the incoming/outgoing network requests for the app. One method is this (completion block is typedeffed):
+(void)checkPlayerCode:(NSString *)playerCode completionHandler:(PlayerCompletionBlock)block {
[self addDownloadSpinner];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/players/%@",kBaseURL, playerCode]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[ServerConnectionRequest sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSDictionary *p = [data objectFromJSONData];
Player *player = [[Player alloc] init];
player.last_name = [p objectForKey:@"last_name"];
if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self removeDownloadSpinner];
block(player);
});
}
}];
}
Which is called from a uiviewcontroller like this:
[ServerRequest checkPlayerCode:codeCell.textField.text completionHandler:^(Player *p){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"wooo block" message:@"works" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}];
However the dispatch_get_main_queue() block never gets fired.