Partials
Partials, sometimes called Spies, are doubles used to verify expectation and, at the same time, behave exactly like the external entity they replace. In most cases a partial is just a subclass of the type it replaces and the main difference is that it implements the same mechanism implemented by mocks to capture arguments.
They are extremely useful when it might be difficult to use another type of testing double, or when the behavior must be maintained.
Since they are subclasses, they have to override methods to capture arguments. When doing so, there’s no need to override all the methods from the superclass, only the methods where arguments need to be captured.
These overridden methods should call the superclass once the arguments are captured, otherwise the partial might end up in an inconsistent internal state, behaving weirdly.
class UINavigationControllerPartial: UINavigationController {
private(set) var lastViewController: UIViewController?
private(set) var lastAnimated = false
override func pushViewController(
_ viewController: UIViewController,
animated: Bool
) {
lastViewController = viewController
lastAnimated = animated
super.pushViewController(
viewController,
animated: animated
)
}
}
Although partials are easy to implement, they still behave as the original type, and that could introduce side-effects such as leaving behind changes to the test environment which could affect tests that will run after.
Where to go from here: