Testing Against Real External Dependencies

Testing Elixir — by Andrea Leopardi, Jeffrey Matthias (22 / 80)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 What Is an Integration Test? | TOC | Dependency Doubles 👉

Many applications interface with databases, other services, or third-party APIs. This makes integration testing harder, since we now have to deal with other running systems in order to design and run our integration test suite. There are two main approaches to dealing with an external dependency in tests: we can either use the real dependency, or we can replace it with something that doubles as the dependency. In this section, we’re going to have a look at how we can test against external dependencies by using the actual dependencies in our test suite, and then we’ll look at dependency doubles in the next section. We’ll also offer some guidance on when each of these solutions is appropriate.

Say our application is backed up by a database. The database is a dependency that’s external to the application, since it’s not running inside it. For our purposes, we can see it as a black box that we can assume works correctly if used correctly. Let’s imagine that our application uses this database to store blog articles and that it provides an HTTP API as the interface for creating articles. We have an Article struct that represents an article:

integration_tests/real_external_dependencies/article.ex

​ ​defmodule​ Article ​do​
​ defstruct [​:title​, ​:body​, ​:author_email​]
​…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.