我一直在尝试对异步请求进行自动化测试,但是在测试功能等待时,我无法在不同的线程中运行任何东西。这是测试功能:
- (void) testBoxManagerConnexionStatus
{
ControlSender* cs = [ControlSender get];
requestShouldSucceed = YES;
[cs startCheckingReachabilityWithDelegate:self];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:TIMEOUT_INTERVAL+1.0]];
STAssertTrue(downloadComplete, @"Download should be over by now");
}
我的测试类以这种方式实现回调方法:
- (void)controlSender:(ControlSender *)controlSender sentSuccessfullyCode:(FreeboxControl)code
{
if (requestShouldFail) {
STAssertTrue(NO, @"Request should have failed");
}
downloadComplete = YES;
}
- (void)controlSender:(ControlSender *)controlSender couldntSendCode:(FreeboxControl)code details:(NSHTTPURLResponse*)details
{
if (requestShouldSucceed) {
STAssertTrue(NO, @"Request should have succeded");
}
downloadComplete = YES;
}
但是,每当我常用的代码尝试在不同的线程中运行某些东西时,什么都不会发生。例如 NSURLConnection 在分配时从不调用它的委托方法:
m_connexion = [[NSURLConnection alloc] initWithRequest:m_networkRequest delegate:self];
既不是-connectionDidFinishLoading:
也不是-connection:didFailWithError:
像这样的电话也是如此:
[self performSelectorInBackground:@selector(BG_startCheckingReachabilityWithDelegate:) withObject:delegate];
运行测试时,Nothings 在后台被调用。不过,相同的代码在测试之外也能正常工作。有没有办法用 OCUnit 测试异步 url 请求?
谢谢您的帮助。