Test an Error Scenario
Once we have a test that calls the data task completion closure, testing error scenarios isn’t hard. We know how to capture the closure, call it with some arguments, and handle asynchronous code. For errors, all we need to do is change the arguments we pass to the completion closure.
When our parsing code detects an error, it displays an alert to the user. We know from Chapter 9, Testing Alerts how to test alerts and that we need the helper framework. Follow Add the Helper Framework to the Project to add ViewControllerPresentationSpy to the test target. Then import ViewControllerPresentationSpy it in ViewControllerTests.swift.
Since we’re adding new tests, let’s move the system under test and the test spy, adding ...
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.