Design the Test Case

Now we can try using our test spy to test the app. 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.

Let’s test that tapping the button creates a data task with the expected request. Think for a moment about how to set up the test and how to confirm the result. We want to do the following:

  • Instantiate the view controller from the storyboard.

  • Create an instance of MockURLSession.

  • Inject the MockURLSession into the view controller. Following what we learned in Test UserDefaults, let’s make sure to do this before loading the view. We do ...

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.