chvanikoff

chvanikoff

How to assert some process received some message?

Hi, I’m in need to test that some process (in my particular case, it’s a Phoenix channel) receives some message from another process (via handle_info). Unfortunately, I didn’t found anything suitable in stdlib and wonder if there’s a common approach for doing this, or maybe I missed something in stdlib? As of now I see 2 ways of testing this: mocking receiver process and watching it’s message queue, but both of the approaches don’t seem to be clear and correct way of doing this.

Most Liked

rvirding

rvirding

Creator of Erlang

Three ways I know you can do this but neither are very straight forward:

  • Use the tracing BIFs to trace messages sent to that process. You can then match on them to get the ones in which you are interested. AFAIK there is no elixir library which supports this so you need to use an erlang interface, for example :dbg.
  • Trace calls to the handle_info/2 function in your module in the process in which you are interested.
  • Use the erlang :sys module to install a special debug handler then turn on tracing with :sys.trace/2. More high level and OTPish, but I have never tested it.

The tracing always works but might be a bit tricky to set up. It also works very well if you want inspect running production systems. Not surprising as that is what the tracing was designed for. :grinning:

josevalim

josevalim

Creator of Elixir

I would prefer not. It must be used rarely and having it as part of the assertions may lead developers to write tests that rely on implementation details (did this process receive a message) instead of asserting on a particular behaviour from receiving that message.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement