Using both ConnCase and ChannelTest on same test

Hi everyone!

I am trying to build some tests making use of one http connection and one phoenix channel. I have used previously both of them separately making use of Phoenix.ConnTest and Phoenix.ChannelTest respectively, but now I need to use both of them someway.

I decided to create a new support test file at *./test/support/channel_and_conn_case.ex * with this code:

defmodule Project.ChannelAndConnCase do
  use ExUnit.CaseTemplate

    using do
      quote do
        defmodule Conn do
          require Phoenix.ConnTest
          @endpoint Project.Endpoint
          defdelegate get(conn, route, params), to: Phoenix.ConnTest

        defmodule Channel do
          require Phoenix.ChannelTest
          @endpoint Project.Endpoint
          defdelegate connect(socket_module, params), to: Phoenix.ChannelTest
          defdelegate subscribe_and_join(socket, channel_module, topic), to: Phoenix.ChannelTest


Using this I am able to call functions from both files (Phoenix.ConnTestandPhoenix.ChannelTest`), but I feel like this is not the way to go. I would like to know if there is a more appropriate approach to get this done.

Thank you in advance!



defmodule Project.MyTest do
  use Project.ConnCase
  use Project.ChannelCase

  test "would it work?" do
    # ...

this work?

1 Like

Thanks for your answer!

I would say not since Project.ConnCase is importing this function connect while ChannelCase is importing this other one connect

Which should be ok if you are not going to use http connect (I don’t remember ever using it in tests). The channel’s one would overwrite it since it’s imported after?

I have tried to compile but it gives this error: function connect/2 imported from both Phoenix.ChannelTest and Phoenix.ConnTest, call is ambiguous.

1 Like

Then I’d modify ConnCase to not import connect/2,3 probably.

import Phoenix.ConnText, except: [:connect] # or whatever the syntax is, maybe `except: [connect: 2, connect: 3]`