Creating and Using Test Doubles in Our Tests
Explore how to create and use various test doubles including mocks, fakes, stubs, and spies in unit testing event-driven Golang applications. Understand how to configure mocks with the Testify package and apply the Arrange-Act-Assert pattern for clearer test structure and better isolation of components.
We'll cover the following...
We will want to use a test double that is not only able to intercept the calls but also able to send back programmed responses. There are several kinds of test doubles, so let’s look at them and see which works best for us here.
Test doubles are tools we can use to isolate the system or code under test from the rest of the system around it.
Types of test doubles
These tools come in different forms, each useful for different testing scenarios:
Fakes: They implement the same functionality as the real dependency. An in-memory implementation of a repository could stand in and take the place of a PostgreSQL implementation so that the test does not rely on any real I/O.
Stubs: These are like fakes, but the stub implementation responds with static or predictable responses.
Spies: They work like an observable proxy of the real implementation. A spy can be used to report back the input, return values, and the number of calls that it receives. Spies can also help with recording the inputs and ...