Understanding when test doubles are appropriate
Mock objects are a useful kind of test double, as we have seen. But they are not always the right approach. There are some situations where we should actively avoid using mocks. These situations include over-using mocks, using mocks for code you don’t own, and mocking value objects. We’ll look at these situations next. Then, we’ll recap with general advice for where mocks are typically useful. Let’s start by considering the problems caused when we overuse mock objects.
Avoiding the overuse of mock objects
At a first glance, using mock objects seems to solve a number of problems for us. Yet if used without care, we can end up with very poor-quality tests. To understand why, let’s go back to our basic definition of a TDD test. It is a test that verifies behaviors and is independent of implementations. If we use a mock object to stand in for a genuine abstraction, then we are complying with that.
...