需要对以下代码进行单元测试,dispatch_async意味着代码不会被应用逻辑序列执行,有关如何使其及时运行的任何想法?
谢谢。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[AdTracker dosomething];
});
答案 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();
}