I've been trying to automate tests on asynchronous requests but I haven't been able to run anything in a different thread while the test function was waiting. Here is the test function:
- (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");
}
My test class implements the callback methods this way:
- (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;
}
But whenever my usual code try to run something in a different thread nothing happens. For example the NSURLConnection never call its delegate methods when allocated:
m_connexion = [[NSURLConnection alloc] initWithRequest:m_networkRequest delegate:self];
Neither the -connectionDidFinishLoading:
nor the -connection:didFailWithError:
Same thing for calls like this one:
[self performSelectorInBackground:@selector(BG_startCheckingReachabilityWithDelegate:) withObject:delegate];
Nothings gets called in background when running the test. The same code works fine outside of the test though. Is there any way to test asynchronous url request with OCUnit?
Thanks for the help.
you could look at https://github.com/danielpunkass/RSTestingKit which has a way to wait on the run loop in unit tests you can see his slides at http://www.red-sweater.com/talks/UnitTesting.pdf for some background. It may have some info to help you get started.