cristian.riano

cristian.riano

Extract common ExUnit setup

Among many of the tests in our codebase there is a setup repeated multiple times, which is basically starting some required processes (mainly Registries) and looks something like this:

setup do
  start_supervised(MyApp.MyModule.Supervisor)
  start_supervised({Registry, keys: :unique, name: MyApp.Registry.ById})
  ...
  :ok
end

I want to extract this in a common module so it can be used easier.

My idea was something like this:

  1. Create a module with setup functions under test/support
  2. Import the module on the required test
  3. Call them with in the test setup like setup :set_my_registries (like defined here

The problem is that the start_supervised function is of course not defined in my module.

My questions are:

  • Can I import/require something from ExUnit to make it work?
  • Should I try to hack it using Macros and use it in the test?
  • Is there a better approach?

PS: I know the philosophy in Elixir tests is to be descriptive but is very annoying when something change names or now there is another dependency and added in all the setup blocks

Marked As Solved

cristian.riano

cristian.riano

So for anyone having a similar issue you can import ExUnit.Callbacks and it should do the trick.
My support module (test/support/my_app_mock.ex) now looks something like this

defmodule MyApp.Mock do
  import ExUnit.Callbacks

  def set_scaffold(_context) do
    start_supervised(MyApp.MyModule.Supervisor)
    start_supervised({Registry, keys: :unique, name: MyApp.Registry.ById})
    ...
    :ok
  end
end

And then it can be used like

...
import MyApp.Mock

setup :set_scaffold

Just don’t forget to add the path to the compilation path list (example here)

Also Liked

drl123

drl123

Can you just define a special case template like conn_case, data_case, etc. that is YourApp.RegistryCase and put that common code in the setup or using block? That way the tests that need it just use YourApp.RegistryCase and those that don’t use the usual ones.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement