用IEx调试phoenix测试

lap*_*ira 2 testing elixir phoenix-framework

我知道您可以使用IEx调试webapp但是可以在测试中设置断点吗?

例如,在下面的测试中,我想检查conn中的内容或检查任何其他变量或宏.

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
Run Code Online (Sandbox Code Playgroud)

为了使它能够与webapp一起使用,你必须运行phoenix.server iex -S mix phoenix.server

但在这种情况下测试不是webapp所以它抱怨:

Cannot pry #PID<0.406.0> at ... Is an IEx shell running?
Run Code Online (Sandbox Code Playgroud)

Koc*_*ber 5

要检查conn结构(或任何其他变量)内部的内容,您可以IO.inspect conn像往常一样使用和运行测试mix test- 这里不需要使用pry.例如:

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

但是,如果你真的需要一个shell,你可以这样调用它:

iex -S mix test 
Run Code Online (Sandbox Code Playgroud)