【问题标题】:Debug phoenix test with IEx用IEx调试phoenix测试
【发布时间】:2018-04-26 00:52:18
【问题描述】:

我知道您可以使用 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

为了让它与 webapp 一起工作,你必须让 phoenix.server 运行 iex -S mix phoenix.server

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

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

【问题讨论】:

    标签: testing elixir phoenix-framework


    【解决方案1】:

    要检查 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
    

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

    iex -S mix test 
    

    【讨论】:

      【解决方案2】:

      ExUnit 测试位于 .exs 文件中,这意味着其中编写了解释代码。所以不能在里面设置断点。

      有关 .exs 和 .ex 文件的更多信息。见Elixir: When to use .ex and when .exs files

      如果你想检查任何地图,只需写下

          IO.inspect conn
      

      对于简单的变量,你可以写

          IO.puts variable_name
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-09
        • 1970-01-01
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        • 2016-10-02
        相关资源
        最近更新 更多