Fl4m3Ph03n1x
How to test side effects on other processes?
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?
Marked As Solved
van-mronov
Even if it looks like you test your implementation of request_fn you can do something like this in your test:
pid = self()
request_fn = fn url ->
IO.puts("url #{url}")
send(pid, :nok)
end
send(params, request_fn: request_fn)
assert_receive :nok
Also Liked
peerreynders
That tactic is incredibly useful in all sorts of situations. Unfortunately there isn’t some catchy name associated with it (“Informant function”?).
That is likely due to the fact that there is a range of potential techniques around assert_receive/3, assert_received/2, refute_receive/3 and refute_receive/2 that
- start simply with the test process injecting it’s pid into the “process under test” to become the recipient of all messages,
- to the test process launching any number of faux processes (or GenServers) to meet the collaboration needs of the “process under test”.
van-mronov
Yeah, I use this pattern often, especially in tests. So, informative name could help to address these techniques. Maybe “Callback notifiers”, I’m not sure.








