Dependency Injection with Monkey Patching

Do you have code that relies on a global variable? Do you have code that is dependent on the filesystem? Have you ever tried to test your database error handling code?

In this chapter, we will examine monkey patching as a way to swap out dependencies during our tests and test in a manner that is otherwise impossible. It doesn't matter if these dependencies are objects or functions. We will apply monkey patching to our sample service so that we can decouple our tests from the database; decouple the different layers from each other and all without resorting to significant refactoring.

In continuing with our pragmatic, skeptical approach, we will also discuss the advantages and disadvantages of monkey patching.

The following topics will be covered in this chapter:

  • Monkey magic—an introduction to monkey patching
  • Advantages of monkey patching
  • Applying monkey patching
  • Disadvantages of monkey patching
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.149.19