Channel Testing - can you setup a channel / socket, do some ConnTest then check for output from the channel?

I am writing my first channel tests.

Up until now I have been using Phoenix.ConnTest very happily.

Is there anyway I can marry both sorts of tests together?

E.g. setup a channel / socket, do some ConnTest related action and then check for output from the channel?

You can use Phoenix.ChannelTest, maybe?

See

Yup, I am using Phoenix.ChannelTest - sorry that wasn’t clear…

I suppose what I am looking for is, /not/ just

test "ping replies with status ok", %{socket: socket} do ...

but

test "ping replies with status ok", %{socket: socket, conn: conn} do ...

You can add a setup block where you build_conn and create a socket.

describe "both conn and socket" do
  setup [:build_conn, :connect_socket]

  test "ping replies with status ok", %{socket: socket, conn: conn} do
    # do tests
  end
end

defp build_conn(_context) do
  {:ok, %{conn: build_conn()}}
end

defp connect_socket(_context) do
  {:ok, %{socket: connect(MyApp.SomeSocket, %{"some" => "params"})}
end

Maybe https://hexdocs.pm/ex_unit/ExUnit.Callbacks.html#content would be helpful as it describes setup in ExUnit among other things.

2 Likes

Oooh, v interesting - thanks!

Fantastic answer. A lot of the macro-mystery of these helpers has lifted now. Very simple to build my own remix of ChannelTest and ConnTest.

Thanks again!