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

com

com

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

Where Next?

Popular in Guides/Tuts Top

bentanweihao
I wrote a thing: http://engineering.pivotal.io/post/how-to-set-up-an-elixir-cluster-on-amazon-ec2/ Hope this is helpful!
New
jswny
Hello everyone, I recently redesigned my entire deployment process for Phoenix apps based on Docker. I really like the strategy that I ca...
New
fmcgeough
pipe into case? I use that fairly frequently…unless I’m misunderstanding what you’re wanting…could be.. its still very early… str = "Hel...
New
marcin
This post is intended to be a guide for others, I was running a remote debugger for the first time and appreciate feedback on how I could...
New
kevinlang
Hey all, With Phoenix 1.6 just around the corner, I figured I’d make a tutorial on how to add Bulma to a new Phoenix 1.6 project. By lev...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
georgeguimaraes
Another cool plugin for Neovim, GitHub - jmbuhr/otter.nvim: Just ask an otter! 🦦 · GitHub makes it possible to run linters for embedded c...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New

We're in Beta

About us Mission Statement