4
votes

I have written an OCUnit unit test case which relies on an external web-service to complete. I know this is controversial in itself but I definitely want to have the web-service included in the tests (it's running on an internal server anyway).

Apparently, the NSURLConnection fails to start when run as part of the SenTestCase. The connection is not started at all by the line:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];

None of the delegate methods are called. Virtually identical code runs normally outside of OCUnit.

What is it about the OCUnit runtime environment that prevents this from running?

It's not that the test completes before the request returns: I have NSCondition locks to handle the asynchronous nature of the request. I've also tried removing these and just spinning on the run loop. The test case continues to execute but the NSURLConnection never starts.

I'd rather not migrate my testing to another Unit Testing Framework if possible.

1
I'm not sure how NSCondition helps unless run the NSURLConnection in another thread. How are you running the run loop?tc.
@Jon Reid: This is not a solution to my problem. The problem is not waiting for the ASynchronous task to complete, it's that NSURLConnection fails to start.Chris Hatton
@TC: The NSCondition lock is just to stop the current thread from exiting the test before the dependent NSURLConnection has had a chance to complete (or even start).Chris Hatton

1 Answers

4
votes

I have exactly the same problem and I resolve it with using

 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:30]];

I use NSCondition before but I think it probably lock the thread of NSUrlConnection.