Mocks
Often, when testing, you need to interact with a method that takes an indeterminate amount of time, such as retrieving data from a database or, worse, retrieving data from an API (that is, over the internet rather than locally from your device).
Calling this kind of method can bring your unit test to a screeching halt, making it almost unusable. To avoid this, we create fake representations of the database or the API using an object called a mock.
Mocks offer two advantages: they respond instantly and, perhaps as importantly, they respond predictably. Once written, they give the same input and mock will always provide the same output.
In order to use mocks, we’ll need to implement dependency injection for some of our classes, so let’s start there.