Asd

Asd

How-to: Async tests with application env.

How to test with application env in async: true

It is quite common for Elixir application to be configured via application env (which is set in config/ files and accessed with Application.get_env and such). However, this application env is global and therefore is very difficult to get right when testing with use ExUnit.Case, async: true.

This article describes how to overcome this problem with Repatch library

Problem

You have a configuration which controls a feature-flag in config/config.exs

import Config
config :my_app, :features,
  lucky_feature_enabled: System.get_env("LUCKY_FEATURE") == "1"

Which controls this simple code

defmodule EightBall do
  def ask(_question) do
    if Application.get_env(:my_app, :features)[:lucky_feature_enabled] do
      Enum.random([:absolutely, :sure, :definitely])
    else
      Enum.random([:no, :no_way, :definitely_not])
    end
  end
end

And you have these tests

test "Enabled feature" do
  Application.put_env(:my_app, :features, lucky_feature_enabled: true)
  assert EightBall.ask("Is it awesome?") in [:absolutely, :sure, :definitely]
end

And

test "Disabled feature" do
  assert EightBall.ask("Is it awesome?") in [:no, :no_way, :definitely_not]
end

Testing with async: true won’t be possible, because if the test of enabled feature test is executed before the disabled test, env will be set and the second test will fail. And executing tests with async: false reduces performance of testing and requires manual cleanup of application env after the test completion.

Solution

Repatch library provides a simple solution to the problem.

  1. First, install it with this code in mix.exs file:

    def deps do
      [
        {:repatch, "~> 1.2"}
      ]
    end
    

    Then mix deps.get

  2. Setup Repatch with this code in test/test_helper.exs file:

    ExUnit.start()
    Repatch.setup()
    Repatch.Application.setup()
    
  3. And just add this into your test file and env will be isolated for each test.

    use Repatch.ExUnit, isolate_env: :local
    

Voila

Now these tests are safe to be used with async: true testing mode. Repatch is a powerful and small library which makes testing in Elixir very simple. Please check out the docs for all available features:

Most Liked Switch mode

com

com

This saved my bacon, testing some app config logic wasn’t going well. Thank you!

Where Next?

Trending in Guides/Tuts Top

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement