Fl4m3Ph03n1x
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
- Why am I getting this warning?
- How can I fix it?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 12 Posts
zwippie
I guess this is what’s happening:
You are defining your test backend modules within
MyApp.DispatcherTest, so their final name is something likeMyApp.DispatcherTest.MyApp.Dispatcher.Backend.MissTest, notMyApp.Dispatcher.Backend.MissTest.Fl4m3Ph03n1x
And how would this confuse the compile exactly ?
Surely, if that was the case the tests should fail, because I am passing the modules directly into
Dispatcher.sendand it wouldn’t even compile, right ?josevalim
This is a (non-solvable) limitation in how the compiler detects that aliases are used. Once that file is compiled, the
Backendhas indeed not been used, because the other modules are only defined (and their contents only executed) when the test runs.You have two options:
warn: falseto the aliasFl4m3Ph03n1x
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
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: falseflag, if that makes any sense.Fl4m3Ph03n1x
The issue I have with putting the modules outside is that as I add more tests and as I need more fake modules that implement the contract, I will end up with big pile of lose modules in my test suite that don’t connect clearly to any test I have - this is why I create the modules inside the test, this way all my tests are self contained.
I can always put the alias inside each module, that also works though. Thanks for chipping in!
LostKobrakai
Do all those modules also have different names? If not I’d consider that an issue, as modules are defined globally on the beam and not per test. If they indeed have unique names then you have a clear connection between test and module.
Fl4m3Ph03n1x
This is a good question. My SUT receives a list of modules that obey a given contract. The SUT then makes calls to those modules.
Ideally I would have each test with 1 or 2 modules that would be the same. But I can’t do that because module names Elixir have to be unique (they are global as you said) so I end up have 2 or 3 tests where I have modules with dumb names like
MyApp.Test1,MyApp.Test2and so on, even if the tests are independent between each other (I cannot repeat a module name in any other test once it was used in a single test because I get theredefining modulewarning).Ninigi
Would it make sense in this case to have one TestBackend module and do the implementation with Mox? You would pass the same module multiple times though, I don’t know if that would makes things clearer, or more obscure.
Fl4m3Ph03n1x
Not a bad idea, but since I need several modules (several mox) I would have to still define them separately:
In the end, I would still end up with the same dummy names problem, but on the flip side I would have less lines of code
Interesting suggestion!