How does one unit test network-dependent operations?

network-programming, octest, unit-testing, xcode

Solution

I posed this question to the venerable folks on #macdev IRC channel on freenode.net, and I got a few really good answers.

Mike Ash (of mikeash.com) suggests implementing a local web server inside my app. For complex cases, I'd probably do this. However, I'm just using some of the built in `initWithContentsOfURL:(NSURL *)url` method of `NSData`.

For simpler cases, mike says an alternate method is to pass base64 encoded dummy data directly to the NSData initializer. (Use `data://dummyDataEncodedAsBase64GoesAfterTheDataProtocolThingy`.)

Similarly, "alistra" suggests using local file URLs that point to files containing mock data.

Problem

Some of my apps grab data from the internet. I'm getting into the habit of writing unit tests, and I suspect that if I write a test for values returned from the internet, I will run into latency and/or reliability problems. What's a valid way to test data that "lies on the other end of a web request"? I'm using the stock unit testing toolkit that comes with Xcode, but the question theoretically applies to any testing framework.

Original source