繁体   English   中英

Phoenix / Elixir:如何使用Plug.Test.conn()在测试连接中设置操作?

[英]Phoenix/Elixir: How to set the action in a test connection with Plug.Test.conn()?

我写了一个小插件,它接受一系列动作作为选项。 当当前调用的动作在此列表中时,插头的行为将有所不同。

为了进行测试,我需要在单元测试中设置操作。 这可能吗? 我没有在文档中找到任何东西。

这是插件文档中给出的简短示例。

defmodule MyPlugTest do
  use ExUnit.Case, async: true
  use Plug.Test

  @opts AppRouter.init([])

  test "returns hello world" do
    # Create a test connection
    conn = conn(:get, "/hello")

    # Invoke the plug
    conn = AppRouter.call(conn, @opts)

    # Assert the response and status
    assert conn.state == :sent
    assert conn.status == 200
    assert conn.resp_body == "world"
  end
end

我会考虑在您的控制器测试中进行集成测试。 由于conn.private存储是为库设计的,因此随时可以更改。

如果您不关心Phoenix中的更改,则可以执行以下操作:

conn =
   conn(:get, "/hello")
   |> put_private(conn, :phoenix_action, :index)
   |> AppRouter.call(conn, [:index])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM