【发布时间】:2016-12-04 00:05:57
【问题描述】:
我写了一个小插件,它接受一个动作列表作为选项。当当前调用的操作在此列表中时,插件的行为会有所不同。
为了对此进行测试,我需要在单元测试中设置操作。这可能吗?我在文档中没有找到任何内容。
这是 Plug 文档中给出的简短示例。
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 = put_private(conn, :phoenix_action, :action_name)(将 :action_name 替换为操作名称)? -
如果没有,你能发布插件的来源吗?我不确定我是否理解这个问题。
-
@Gazler 我的意思是控制器操作(新建、创建、索引等)。当我将选项
except: [:create, :new]传递给插件时,它不应该在操作为:create或:new时进行一些检查。因此,为了测试这一点,我需要能够在我的测试中调用特定的操作。 -
@Dogbert 谢谢,成功了!