How can you test joining a Phoenix Channel?

Hi, I’m just going through the documentation on testing Phoenix Channels and it didn’t cover testing the join callback. When clients join my channel I respond with some data based on the state of the client that’s joining, and I’d like to test a few scenarios. Any thoughts on how I might go about that?


Never really try it yet, but the second value in the tuple to subscribe_and_join/3 is the reply. Maybe can check it out first.

@nthock that’s a good point. I guess there’s nothing stopping me from doing the stuff that’s in the test setup block in a proper test block. Something like:

  test "join topic" do
    |> expect(:get, fn _key ->
      %{"messages" => []}

    {:ok, chat_join_reply, socket} =
      socket(CardsWeb.UserSocket, nil, %{session_id: @session_id})
      |> subscribe_and_join(CardsWeb.ChatChannel, "chat:abc123")

    assert chat_join_reply.body.chat_state == %{"messages" => []}

Thanks, this seems to work well.