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?

https://hexdocs.pm/phoenix/testing_channels.html

Thanks

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.

https://hexdocs.pm/phoenix/Phoenix.ChannelTest.html#subscribe_and_join/4

@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
    CardsStoreMock
    |> expect(:get, fn _key ->
      %{"messages" => []}
    end)    

    {: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" => []}
  end

Thanks, this seems to work well.