Search code examples
testingelixirphoenix-framework

Debug phoenix test with IEx


I know you can debug the webapp with IEx but is it possible to set a breakpoint inside a test?

For example in the following test I would like to check what's inside conn or check any other variable or macro.

defmodule HelloWeb.PageControllerTest do
  use HelloWeb.ConnCase
  require IEx
  test "GET /", %{conn: conn} do
    IEx.pry
    conn = get conn, "/"
    assert html_response(conn, 200) =~ "Welcome to Phoenix!"
  end
end

In order to make it work with the webapp you must have phoenix.server running with iex -S mix phoenix.server

But in this case is a test not the webapp so it complains with:

Cannot pry #PID<0.406.0> at ... Is an IEx shell running?

Solution

  • To check what is inside conn structure (or any other variable) you can just use IO.inspect conn and run tests as usual with mix test - there is no need to use pry here. For example:

    defmodule HelloWeb.PageControllerTest do
      use HelloWeb.ConnCase
      test "GET /", %{conn: conn} do
        IO.inspect conn, limit: :infinity
        conn = get conn, "/"
        assert html_response(conn, 200) =~ "Welcome to Phoenix!"
      end
    end
    

    However, if you really need a shell you can then call it like this:

    iex -S mix test