Mocking external dependencies
Mocking is useful for simulating the behavior of dependencies without actually relying on them. This is powerful because it allows us to test our application in isolation from its environment. The reason we want to do this is to have tests that ensure that the application’s code works correctly, irrespective of the state of its dependencies.
The role of mocking
Let me clarify this with an example. Let’s say you have a method that stores a bookmark in the database. You write a test method to verify that, and it fails. You run it again, and it passes. Can you tell, without investigating, whether this was due to a transient database connectivity problem or due to a bug in the code? You can’t! But if you remove the dependency (that is, the database) from the equation and the same behavior occurred, you can tell (with a high level of confidence) that this was due to a bug in the code.
It is worth mentioning that we usually write...