Isolating Code

We can employ multiple strategies to test our code in a way that removes outside variables, controlling the situation in which our code under test must perform and allowing us to expect a specific outcome. We’ll cover the easiest method—injecting dependencies and creating basic substitutes (test doubles)—in order to add another option to your testing tool belt.

Dependency Injection

Before we can leverage dependency injection to isolate the behavior of our code under test, let’s take a moment to define dependency injection. A dependency is any code that your code relies on. Dependency injection (often abbreviated as DI) is a fancy name for any system that allows your code to utilize a dependency without hard-coding the name of ...

Get Testing Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.