Dependency Doubles
Testing Elixir — by Andrea Leopardi, Jeffrey Matthias (23 / 80)
👈 Testing Against Real External Dependencies | TOC | Interfacing to External Dependencies with Beha viours 👉
In this section, we’re going to see how to create something that acts as an external dependency that we don’t have control over. This will allow us to test the code that interacts with the external dependency without using the dependency at all.
There’s one disclaimer to make before diving in deeper: the terminology around dependency doubles is not “standard”: different people use these terms in different ways. We’ll use the terms double, mock, stub, and others in the way that we think makes the most sense and we’ll use them consistently, but you might encounter the same concepts under different names outside of this book. We’ll define these terms later on in this chapter. With that out of the way, let’s get to it.
The component that acts as the external dependency is often referred to as a double of that dependency. The name comes from the fact that, to the callers of the dependency, the dependency double looks exactly like the dependency. The implementations of dependency doubles can vary in complexity. Sometimes, a dependency double just looks like the external dependency but doesn’t do anything. Other times, the dependency double can be used to gain insight into how the dependency is called and possibly to test the interaction as well. Let’s dive into how we can use dependency doubles…