Fl4m3Ph03n1x

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:

  1. I cannot run the tests asynchrousnly
  2. 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 Switch mode

van-mronov

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

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

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.

Where Next?

Trending in Questions Top

jonnycharles
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
spammy
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
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
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
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
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
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement