Hello everybody,
I’m following along the Programming Phoenix 1.4 Book.
And at a moment we have the following simple code that do an HTTP request using erlang :httpc
(excerpt simplified):
defp fetch_data(url) do
{:ok, res} = :httpc.request(url)
res
end
And for testing purpose, we want to mock the HTTP request from :httpc
.
But for that we defined in the regular code itself a “conditional” use of the mock instead of the regular library when the code is running in the tests.
So we first defined a config option for the test environment in config.test.exs
like so:
# config/test.exs
config :info_sys, :wolfram, http_client: InfoSys.Test.HTTPClient
That it triggers the mock HTTPClient in the module InfoSys.Test.HTTPClient
when running in the test environment thanks to the following code in the code (located in a module named Wolfram in the example):
# lib/info_sys/wolfram.ex
@http Application.get_env(:info_sys, :wolfram)[:http_client] || :httpc
defp fetch_data(url) do
{:ok, res} = @http.request(url)
res
end
So we’re using :httpc
in all environment but the test environment.
Now the mock HTTP client is in an Elixir Script file located somewhere within the test directory:
# test/backends/http_client.exs
defmodule InfoSys.Test.HTTPClient do
def request(url)
...
data
end
end
In order to this to work we carefully included the code to be required in top of the test_helper.exs
file:
# test/test_helper.exs
Code.require_file("backends/http_client.exs", __DIR__)
ExUnit.start()
Now everything work but I always get the following warning:
warning: InfoSys.Test.HTTPClient.request/1 is undefined (module InfoSys.Test.HTTPClient is not available or is yet to be defined)
lib/info_sys/wolfram.ex:4 InfoSys.Wolfram.fetch_data/1
Here I put back the code where the warning is located:
# lib/info_sys/wolfram.ex
@http Application.get_env(:info_sys, :wolfram)[:http_client] || :httpc
defp fetch_data(url) do
{:ok, res} = @http.request(url) # Warning on this line
res
end
This warning really bothers me and I didn’t find a way to get rid of the warning.
I guess that this is related to the lack of .beam
file generated for .exs
files.
So I even tried to change the mock module inside a .ex
file, but it seems that since it’s in the test folder it didn’t get compiled and that I still to use the Code.require_file
to make it work.
Has anyone have any idea on how to clean up this warning?
Thank you very much.