对dispatch_async之类的代码进行单元测试。

时间:2012-07-10 12:19:34

标签: testing dispatch-async

需要对以下代码进行单元测试,dispatch_async意味着代码不会被应用逻辑序列执行,有关如何使其及时运行的任何想法?

谢谢。

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [AdTracker dosomething];
    });

1 个答案:

答案 0 :(得分:2)

请参阅http://www.mikeash.com/pyblog/friday-qa-2011-07-22-writing-unit-tests.html

+ (BOOL)waitFor2:(finishBlock)block {
NSTimeInterval timeoutInSeconds = 10.0;
NSDate* giveUpDate = [NSDate dateWithTimeIntervalSinceNow:timeoutInSeconds];

while (!block() && ([giveUpDate timeIntervalSinceNow] > 0)) {
    NSDate *stopDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
    [[NSRunLoop currentRunLoop] runUntilDate:stopDate];   // un-blocking.
    DLog(@"+++++  %@", [NSDate date]);
}   

return block();

}