For some reason, I want to broadcast a message when someone leaves a channel or disconnects due to some network issues.
I wrote this code:
defmodule MyAppWeb.SomeChannel do
use MyAppWeb, :channel
@impl true
def terminate(_reason, socket) do
broadcast(socket, "left", %{})
end
end
And here’s the test:
test "broadcast when leave", %{socket: socket} do
IO.inspect self()
leave(socket)
assert_broadcast("left", _)
end
When I ran this test, I got the following error with no additional detail:
#PID<0.875.0>
** (EXIT from #PID<0.875.0>) shutdown: :left
I tested my implementation using the Chrome websocket extension “WebSocket King Client”, and it works.
My question is, how to test broadcasting in terminate/2
, or is there another way to implement this behavior that can be tested?