Mocks and stubs
While writing tests, sometimes we need to abstract away or mock some parts of our system in order to make testing possible and the results reproducible. Mocks are mainly used for unit testing. As we've mentioned, unit tests must work in complete isolation, which means that when we test a specific unit of code (for example, a function), then all of its dependencies should be controlled and should not cause side effects.
Mocks are imitations or fake units (that is, objects, variables, functions, and so on) that simulate the behavior of real units. We can use mocks in order to control the dependencies used by the code that we want to test. For example, if we want to write unit tests for the population service of WorldExplorer ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access