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.

mushu8

mushu8

For later reference, trace messages receive GenServer call methods :

test "call test" do
  pid = start_link_supervised!({MyGenServer})
  :erlang.trace(pid, true, [:receive])
  MyGenServer.that_sends_call() # implementation sends a call message {:call_mesage_name, 1}

  assert_receive {:trace, ^pid, :receive,
    {:"$gen_call", pid, {:call_mesage_name, parameter}}
  }
  assert parameter == 1
test "cast test" do
  pid = start_link_supervised!({MyGenServer})
  :erlang.trace(pid, true, [:receive])
  MyGenServer.that_sends_cast() # implementation sends a cast message {:cast_mesage_name, 1}

  assert_receive {:trace, ^pid, :receive,
    {:"$gen_cast", pid, {:cast_mesage_name, parameter}}
  }
  assert parameter == 1
gregvaughn

gregvaughn

I know of 2 ways. The first, and the one I prefer, is for the test itself to send in the self pid to be the receiver of the message. Then you can use ExUnit’s assert_received. The other approach is to, only in test code, use :erlang.process.info

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement