From the course: Node.js: Testing and Code Quality
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Replacing code with test doubles
From the course: Node.js: Testing and Code Quality
Replacing code with test doubles
- [Presenter] Unit testing requires isolation of the target code. However, due to the power of (faintly speaking) Modularity, pretty much, everything depends on everything. How can we isolate our code, so we only test the intended functionality? There's a solution to this problem, and it involves replacing and inspecting code execution with test structures, known as stubs, spies and mocks. In this chapter, we're going to start by discussing what Test Doubles are, and how they can be used to replace code. Then, we'll compare some techniques for overriding dependencies by manipulating the required cash. With that foundation, we'll apply these techniques to our testing. We'll control the responses of methods, observe interactions between components and verify expected behavior of the code we're testing. We'll do all of this, without having to modify how our modules require code, which is fantastic. Let's start by…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Replacing code with test doubles6m 51s
-
(Locked)
Mocking functions for test code7m 1s
-
(Locked)
Writing your first Jest mock5m 46s
-
(Locked)
Mock an entire module with Jest7m 22s
-
(Locked)
Mocking module dependencies6m 19s
-
(Locked)
Spying to observe interactions7m 3s
-
(Locked)
Challenge: Mock a successful reservation creation53s
-
(Locked)
Solution: Mock a successful reservation creation6m 11s
-
(Locked)
-
-