6 Testing frameworks, mocking, and dependencies

 

This chapter covers

  • Isolating code from external dependencies by using interfaces
  • Injecting dependencies into services to create composable portions of code
  • Constructing suites of tests to reduce boilerplate setup and teardown for tests
  • Stubbing and mocking dependencies to create isolated tests that are reliable
  • Simulating calls to an external service to test the logic of a client service

“So you want to sit with me and tell me what to code? Won’t that be a waste of your time?” You’ve just given the intern permission to access the repository and sat down next to her at her desk. You explain that she should download the repository and follow the directions on the README, and you will sit there to answer any questions. After making some minor notes on where to expand the documentation, you move on to coding. The intern will add some new functionality to the system and write the code while you explain what to write.

6.1 Dependency inversion principle

6.2 Defining an interface

6.3 Dependency injection

6.4 Testing stubs

6.5 Mocking

6.5.1 Setting up our test suite

6.5.2 Using our mocks in test

6.6 Fake

6.7 Just the base of the pyramid

Summary