【发布时间】:2016-02-19 00:16:39
【问题描述】:
我想测试一个调用异步任务的函数(对 web 服务的异步调用):
+(void)loadAndUpdateConnectionPool{
//Load the File from Server
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *responseCode, NSData *responseData, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)responseCode;
if([httpResponse statusCode] != 200){
// Show Error
}else{
// Save Data
// Post Notification to View
}
}];
}
由于该函数没有完成处理程序,我如何在我的 XCTest 类中测试它?
-(void)testLoadConnectionPool {
[ConnectionPool loadAndUpdateConnectionPool];
// no completion handler, how to test?
XCTAssertNotNil([ConnectionPool savedData]);
}
是否有任何最佳做法,例如超时或其他什么? (我知道如果不重新设计loadAndUpdateConnectionPool 函数就无法使用dispatch_sempaphore)。
【问题讨论】:
标签: ios objective-c xcode xctest