3 Breaking dependencies with stubs

 

This chapter covers

  • Types of dependencies—mocks, stubs, and more
  • Reasons to use stubs
  • Functional injection techniques
  • Modular injection techniques
  • Object-oriented injection techniques

In the previous chapter, you wrote your first unit test using Jest, and we looked more at the maintainability of the test itself. The scenario was pretty simple, and more importantly, it was completely self-contained. The Password Verifier had no reliance on outside modules, and we could focus on its functionality without worrying about other things that might interfere with it.

In that chapter, we used the first two types of exit points for our examples: return value exit points and state-based exit points. In this chapter, we’ll talk about the final type—calling a third party. This chapter will also present a new requirement—having your code rely on time. We’ll look at two different approaches to handling it—refactoring our code and monkey-patching it without refactoring.

3.1 Types of dependencies

3.2 Reasons to use stubs

3.3 Generally accepted design approaches to stubbing

3.3.1 Stubbing out time with parameter injection

3.3.2 Dependencies, injections, and control

3.4 Functional injection techniques

3.4.1 Injecting a function

3.4.2 Dependency injection via partial application

3.5 Modular injection techniques

3.6 Moving toward objects with constructor functions

3.7 Object-oriented injection techniques

3.7.1 Constructor injection

3.7.2 Injecting an object instead of a function

3.7.3 Extracting a common interface

sitemap