dimitrije

dimitrije

Help on how to prevent function from executing in test environment

Hi all,
I need help on how to not execute a function in test environment.
I have a function which is a background supervised task which executes everytime an object is inserted/updated, because each time update/insertion I need to configure new config string. In test I rely on database, because I need to verify all the updates and insertions are valid.
The problem is after a few running tests, the database is blocked or the process is held back and already started, and the next test cannot checkout the repo.
So I need that background function just return nil when running test.
Currently I have solved it with if Mix.env != :test, do: function_call(), but does not seem right.

Does somebody have any experience with these kind of problem ?

First Post!

mindok

mindok

As usual, the right answer depends on the actual circumstances, expected lifetime of the codebase, who is likely to work on it in the future, whether the correct execution of the background task is important enough that it warrants testing.

Here are a couple of other approaches I can think of to disable the code. The first is a very minor change to your approach that may make it more readable to someone in the future.

  1. Use a config option to enable or disable the function call. For example, in config\dev.exs, have a line along the lines of config :my_app, enable_background_thing: true and in config\test.exs have config :my_app, enable_background_thing: false. Then your if Mix.env != :test, do: function_call() approach would be replaced with if Application.get_env(:my_app, : enable_background_thing), do: function_call()

The intent is clearer than your current approach I think.

  1. Use some kind of dependency injection to inject a background processing module which is set up in the config for each environment. e.g.
defmodule TestBackgroundDoer do
  def do_the_thing() do
    IO.inspect("We just pretended the thing was done")
  end
end

defmodule ActualBackgroundDoer do
  def do_the_thing() do
    whatever()
    and_the_other_thing()
  end
end

defmodule BackgroundDoer do
  @doc """
  This is what's called every time object inserted/updated
  """
  def do_the_thing() do
    doer_implementation = App.get_env(:my_app, : doer_implementation)
    doer_implementation.do_the_thing()
  end
end

#dev.exs & prod.exs
config :my_app, doer_implementation: ActualBackgroundDoer

#test.exs
config :my_app, doer_implementation: TestBackgroundDoer

This is probably overkill, but if your background task is is actually multiple for different situations, it is probably better to have a more structured way to mock it out like this so you can check that the right
function is called in the right situation in your tests.

To get your tests to run without disabling the background task, have you worked through the options discussed here: Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.10.1?

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement