Exploring mocks
In this section, we will explore one of the mechanisms that allows us to test code that relies on dependencies. We will see how to use and generate mocks, allowing us to verify the UUT in isolation from the behavior of its dependencies.
Mocks are sometimes known as test doubles and are a simple but powerful concept. They satisfy the interfaces but are fake versions of the real implementations. We have full control over these fake implementations, giving us the freedom to control their behavior. However, if the real implementation changes and our mocks do not, then our tests will give us false confidence.
In Go, we have the following different mocking options:
- Function substitution: This means sending replacement fake functions to the UUT. This is easy to do in Go, which has native support for higher-order functions. We can override function variables and parameters, replacing the behavior of the UUT.
- Interface substitution: This means injecting fake...