Fl4m3Ph03n1x

Fl4m3Ph03n1x

Wrong unused warnings in Elixir

Background

I have a test that defines modules which use a behaviour. To save some typing I am using an alias for the behaviour module when I use it in my tests. However the compiler doesn’t see it and plasters my screen with unused warnings.

Code


defmodule MyApp.DispatcherTest do
  use ExUnit.Case

  # Contract 
  alias MyApp.Dispatcher.Backend
  
  # SUT
  alias MyApp.Dispatcher

  test "calls request_fn with the correct parameters" do

    defmodule MyApp.Dispatcher.Backend.MissTest do
      @behaviour Backend

      @impl Backend
      def match(_params), do: false

      @impl Backend
      def url(_params), do: "/bfg_division"
    end

    defmodule MyApp.Dispatcher.Backend.HitTest do
      @behaviour Backend

      @impl Backend
      def match(_params), do: true

      @impl Backend
      def url(_params), do: "/family_jules"
    end

    defmodule MyApp.Dispatcher.Backend.HitTest2 do
      @behaviour Backend

      @impl Backend
      def match(_params), do: true

      @impl Backend
      def url(_params), do: "/comatose"
    end

    params = %{}
    test_pid = self()
    deps = [
      backends: [
        MyApp.Dispatcher.Backend.HitTest,
        MyApp.Dispatcher.Backend.HitTest2,
        MyApp.Dispatcher.Backend.MissTest
      ],
      request: fn(name, url) ->
        send(test_pid, {:fire, {name, url}})
        {:ok, :received}
      end
    ]

    Dispatcher.send(params, deps)

    expected_name = :hit_test
    expected_url = "/family_jules"
    assert_receive {:fire, {^expected_name, ^expected_url}}

    expected_name2 = :hit_test2
    expected_url2 = "/comatose"
    assert_receive {:fire, {^expected_name2, ^expected_url2}}
  end

end

Inside the test I have 3 test modules (MissTest, HitTest, HitTest2), which implement the behaviour Backend. Each module implements 2 functions to comply with the contract of the behaviour.

Problem

The problem is that every time I run mix test I get this warning plastered onto my screen:

warning: unused alias Backend
  test/myapp/dispatcher_test.exs:4

Which is not true. To prove it, if I actually remove the line alias MyApp.Dispatcher.Backend then I get even more warnings, stating that behaviour Backend doens’t exist or is not specified.

Questions

  1. Why am I getting this warning?
  2. How can I fix it?

Marked As Solved

josevalim

josevalim

Creator of Elixir

This is a (non-solvable) limitation in how the compiler detects that aliases are used. Once that file is compiled, the Backend has indeed not been used, because the other modules are only defined (and their contents only executed) when the test runs.

You have two options:

  1. Pass warn: false to the alias
  2. Move your module definition outside of the test and inside the module body

Also Liked

zwippie

zwippie

I guess this is what’s happening:

You are defining your test backend modules within MyApp.DispatcherTest, so their final name is something like MyApp.DispatcherTest.MyApp.Dispatcher.Backend.MissTest, not MyApp.Dispatcher.Backend.MissTest.

Fl4m3Ph03n1x

Fl4m3Ph03n1x

Thank you so much for your reply.
One final question.

Does this mean, in your opinion, that I am using a anti-pattern in my tests? Am I structuring my code so bad that I am hitting a compiler limitation? (Does this raise any flags to you?)

josevalim

josevalim

Creator of Elixir

I personally don’t think it is a bad flag, it is fine. But you can also move the modules out and the behaviour should be the same, so I would rather do that rather than use the warn: false flag, if that makes any sense.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement