Fl4m3Ph03n1x

Fl4m3Ph03n1x

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?

Showing Posts 1 to 4

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
Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

Brilliant solution!

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews