TDD with the Open-Closed Principle
The OCP was first defined by Bertrand Meyer, but in this chapter, we will follow the later version defined by Robert C. Martin, which is also called the polymorphic OCP.
The OCP states that objects should be open to extension and closed to modification. The aim is that we should be able to modify the behaviour or a feature by extending the original code instead of directly refactoring the original code. That’s great because that will help us developers and testers be more confident about the ticket we’re working on, as we haven’t touched the original code that might be used somewhere else – less risk of regression.
In our ToyCarCreateTest
class, we are stubbing a validator object because we have not written a concrete validator class yet. There are a lot of different ways of implementing validation, but for this example, we’ll try to make it very simple. Let’s go back to the code and create a validator...