views:

58

answers:

1

I'm using the unit testing tools provided in the Google Toolbox for Mac and I want to unit test some code involving NSURLConnections. I believe I need to setup my own NSRunLoop if I want to be able to do this.

What is the proper way to setup such a run loop?

A: 

The only reason I can think to do that would be if you want to do a functional test of the actual asynchronous connection. That's going to be pretty fragile, and slow down your test bundle.

A better approach is to write unit tests of your implementations of NSURLConnection's delegate methods, like connection:didFinishLoading: (for success cases) and connection:didFailWithError: (for error cases)

I generally set up sample response values and verify that my delegate handles them correctly. For example:

    NSString *responseJSON = @"{\"remove\": [123,432], \"new\": [1,2]}";
    NSData *responseBytes = [responseJSON dataUsingEncoding:NSUTF8StringEncoding];

    // in requestHandler, data would have been set by calls to connection:didReceiveData:
    [requestHandler setData:responseBytes];

    [requestHandler connectionDidFinishLoading:nil];

    // set expectations here for what you'll do with the response
    ...
chrispix