5.4 Using Dependencies
Modern test frameworks provide the option of defining spies, stubs, and mocks, which are all also called test doubles. You can think of this as a function whose calls you can check (using the spy), whose behavior you can predefine (using the stub), or whose interaction rules you can define and then check whether they have been adhered to at the end of a test (using the mock). Spies and stubs in particular are very useful when it comes to formulating unit tests. If you use callback functions, such as event handlers, you can’t avoid using spy functions.
In the following sections, we’ll focus on the use of stubs to write unit tests for functions that have additional dependencies. In a unit test, you focus on a specific ...
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