Background
I have a module that makes a async request to another service. This async request is made via Task.start
and I literally don’t care if the request fails or not, I only want to make it.
My objective here, is to test whether or not I am making the request with the correct parameters.
Code
The function send
takes in a params
string and an options keyword list. This keywords list allows me to inject the function that actually makes the HTTP GET request opts[:request_fn].()
.
def send(params, opts \\ []) do
Task.start(fn ->
params
|> build_url
|> opts[:request_fn].()
end)
end
defp build_url(params) do
"www.google.com/search?" <> params
end
Problem
The problem here is that there is no way for me to test whether or not the newly created process is doing what it is told. it may be called opts[:request_fn].()
with the wrong parameters, or it may not be calling it at all.
Even if :request_fn
is set to fail:
request_fn = fn url ->
IO.puts("url #{url}")
assert 1 ==2
throw(:nok)
end
The tests still complete successfully because the error is in another process and not in the one running the test.
Possible solutions
A possible solution came to me from reading a blog on Elixir testing. It pretty much boils down to creating a GenServer
with a publish and subscribe setting, and then making the newly created task send a message to the server’s mailbox. I would then have the test process subscribe to said mailbox and verify that a given message was received.
This has a few problems:
- I cannot run the tests asynchrousnly
- I find it overkill to create a whole pub/sub server and to add that logic to my application only so I can test it
And all this because elixir provides no easy way to just spy on a function and check if it was invoked or not.
Questions
Without spies, how do you test for side effects on other processes that don’t communicate with you?