Assert receive gen_cast


I am very new to elixir, so please forgive my choice of terminology, and if they don’t make sense. Please ask for more details if it can help.

I have a function using GenServer.cast, like below:

def write(some_server, value) do
GenServer.cast(some_server, {:write_value, value})


def handle_cast({:write_value, value}, state) do
{:noreply, state}

I call the “write” from another handle_cast:

def handle_cast({:something}, state) do
{:noreply, state}

Now, how can I use assert_receive to verify the :write has happened in the second handle_cast (assuming I am sure the handle_cast is running)?


I think :sys.get_state can help You in test by ensuring cast are executed…

Something like that…

      MatchEngine.accept_reply request.uuid, @mock_sender, @mock_receiver
      :sys.get_state ReplyServer
      assert length(MatchEngine.list_of_replies) == 0

You can look at

A little tangent to your question, but I’d consider a simple refactoring. Make the something handle_cast function call send_data directly instead of calling the client write function. It’s unconventional for a genserver to call its own client functions. If you really need your genserver to send itself a message, then I’d prefer to see that more explicit so that the handle_cast for something calls GenServer.cast(self(), {:write_value, "test"}

It is not its own client, it is in another GenServer (I am working on someone else’s project, why it is structured like this is another question)