gaggle

gaggle

Automating Tests in Elixir Projects

This post explores different ways to do test automation in Elixir, focusing on how to handle dependency injection — covering patterns, libraries, challenges, and trade-offs along the way. It walks through a series of real attempts using tools like Mock, Mox, ProcessTree, Hammox, and Double, all in the pursuit of fast, simple, and frictionless testing. Part of an ongoing series to explore the “perfect” Elixir setup.

Most Liked Responses

D4no0

D4no0

If you plan on using separate modules, yeah, but generally you have a 2 module setup: the one that calls the external api and the one that does mocks.

Give it a try for sure. This is no new concept and it’s much more easier to reason about compared to the complex setup that mocks require.

Mocking internal logic for me is a hard no, this is one of the big reasons I try to avoid mocks whenever I can. The point of tests is to ensure correctness of your system at different levels. If you are mocking actual business logic, then you are introducing tests that can result in false positives which may cause more problems than they solve, as the code is a dynamic media that is always changing.

Schultzer

Schultzer

Mocking internal code is a huge red flag, it tells everyone that there is fundamental architectural problem, or lack of experience.

In the end, programming is all about data transformations. To put it simple: input → output

So when we test, then we want to assert that we got the expected output from the input.

With that in mind we can design our system in such a way that we only care about changing the input, that input can come from a lot of places.

I saw you mention unit testing and E2E. People tend to conflate all these terms. 99% of the time programmers write integration test, E2E test is a subset of integration test. All your Ecto test are integration test. If a function has side effects then the only way to test it is by an integration test.

Unit test requires a function without side effects.

Regardless of this pedantickery lets just agree that tests should be simple to reason about and if you feel that mocking is the right answer, then you properly have a bigger problem at hand.

LostKobrakai

LostKobrakai

That approach is also what is presented in this great talk: Boundaries

I personally like this approach quite a lot.

Where Next?

Popular in Blog Posts Top

jordiee
https://medium.com/@jpiepkow/distributed-state-is-hard-5a0d384c2f3c
New
JKWA
I didn’t have room for this in my book, Advanced Functional Programming with Elixir , but I still thought it was worth sharing on my blog.
New
lawik
We all have varying degrees of exposure to Big Tech. Some of it seems fine, stable and can be relied on. Some of it feels like shifting s...
New
fredwu
Hi folks, I wrote a blog post the other day on how I built my MVP in 3 months whilst having a day job, using Elixir/Phoenix/LiveView. Th...
New
danschultzer
How to set up the Content Security Policy header with Phoenix LiveView and support inline styles and scripts with CSP nonces.
New
AstonJ
Update: How to use the blogs section You can post in one of the Official Blog Posts threads (like this one), or, via Devtalk and a new t...
New
zacksiri
In December of 2023 we came to the realization that we needed to build our own image server. After hitting a few snags we decided that it...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
axelson
I describe how we use Hot Reloading with Webpack to develop faster and show how to integrate Webpack 5, webpack-dev-server, and Phoenix f...
New
shanesveller
Introduction and first content post in my new series about running Elixir Phoenix apps on Kubernetes are live! I’m describing an opiniona...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement