Design the Test Case
Now we have the tools we need to start testing the network response. Let’s start by adding a test suite ViewControllerTests. Use Test Zero as temporary scaffolding to confirm that you hooked up the test suite. (See Start from Test Zero.) Delete Test Zero once you see its expected failure message.
We want to test how the code handles different network responses. This could include error scenarios, but for now let’s concentrate on the happy path. Think for a moment about how to set up such a test. We want to do the following:
-
Instantiate the view controller from the storyboard.
-
Create an instance of SpyURLSession.
-
Inject the SpyURLSession into the view controller. Make sure to do this before loading the view to avoid ...
Get iOS Unit Testing by Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.