Growing Object-Oriented Software, Guided By Tests (chapter 8)

Feb 11, 22

Very short chapter today - which is a relief, as the last 2 have been absolute units!

unit

When using 3rd party libraries - only mock objects that you control!

We should write an adapter layer to standardize the interface between our code and the imported code.

We should be testing these interfaces with focused integration tests (try to stay flexible as we get a better understanding of the imported code.

This adapter approach allows us to interface with external libraries on our terms.

There is a slight caveat to the above point about “only mocking objects you control” - it can be useful to use mocks when it is hard to simulate some behaviour (a sequence of calls with a transaction that should be rolled back on failure, or an exception). However, these tests should be small in number within the test suite.

Another good example of when to mock with 3rd party libraries is when a call back is fired that needs to be handled by our application code. We can test the adapter/3rd party code, and mock the receiver of the call back to ensure it fired off and was handled by our application code correctly.