18.4 Mocking
It’s very hard, if not impossible, to unit-test a large piece of code that has dependencies. (What’s large? Any code we can’t see entirely without scrolling down in an editor—no, don’t make the font size smaller now.) One advantage of unit testing is that it forces us to make the unit of code smaller. Smaller code is cohesive code. It also forces us to decouple the code from its surroundings. This means less coupling. Higher cohesion and lower coupling are qualities of good design. In this section we’ll discuss ways to deal with dependencies; ways to unit-test code with dependencies are covered in the rest of this chapter.
Coupling comes in two forms: code that depends on our code, and code that our code depends on. We need to ...
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