Unit Testing Asynchronous Operations with OCTest

I have experienced problems while I write the unit tests for a RESTLike web service client. The client class is basically a wrapper around NSURLConnection which asynchronously fetch/download data from a server. Unit tests are synchronously operations and will not work in this context.

The test case is the following: connect to a server via its URL and fetches some data from it. I have used a loop while the downloading operation continues to force the test method to wait for the connection class to finish its job. Following is the test case method:

- (void)testConnection {
    // the custom strategy class to encapsulate the behavior of the rest client
    WSRestClientDefaultStrategy* strategy = [[WSRestClientDefaultStrategy alloc] init];
    // create the rest client under testing
    WSRestClient* client = [WSRestClient restClientWithStrategy:strategy withMethodName:@"anymethod" andParams:nil andHttpMethod:@"GET"];
    [client execute];
    [strategy release];

    // this is a workaround in order to test async requests
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];

    // wait while the connection class does its job
    while ([client loading] && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
}

Please note that the unit test normally should not fetch any data from any external service. Please consider to use mock classes to reduce the dependency. You can use a framework like OSMock for creating the mock classes