Test Code-Based Modal Presentation
Let’s continue to test code-based navigation, this time moving to modals. The view controller makes a present(_:animated:) call. How can we capture the result of this call?
If we had defined the main view controller outside of a storyboard, there’s a useful trick we could have used. To intercept the call, we could make a test-specific subclass. It would provide its own implementation of the present(_:animated:completion:) method, including the optional completion handler.
| // We can't use this for a view controller that comes from a storyboard. |
| private class TestableViewController: ViewController { |
| var presentCallCount = 0 |
| var presentArgsViewController: ... |
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.