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?
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
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.