Replace the Difficult Dependency with a Mock Object
This chapter focuses on refactoring ChangePasswordViewController, so we want to bring it under test. But the class has an embedded dependency on PasswordChanger. Even though PasswordChanger is a simulation, we need to break this dependency. We shouldn’t have unit tests call an actual web service, not even one set up for testing. It’s unreliable, too slow, and would pollute the service.
Instead, let’s apply Extract a Protocol to Support Test Doubles. In PasswordChanger.swift, define an empty protocol named PasswordChanging:
| protocol PasswordChanging {} |
Then in ChangePasswordViewController, change the passwordChanger property to have an ...
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.