我在Phoenix应用程序中有以下模块。这段代码基本上是共享的规范示例。它看起来像这样:
defmodule TattooBackend.CurrentPasswordRequiredTest do
@moduledoc """
This module defines tests that are testing api current password requirement.
"""
import Phoenix.ConnTest
alias TattooBackend.Web.Router
defmacro test_current_password_required_for(method, path_name, action) do
quote do
test "without current password", %{conn: conn} do
method = unquote(method)
path_name = unquote(path_name)
action = unquote(action)
account = insert(:account)
assert make_unauthenticated_request(conn, @endpoint, method, path_name, action, account) == %{
"error" => "Błędne hasło."
}
end
end
end
def make_unauthenticated_request(conn, endpoint, method, path_name, action, account) do
path = apply(Router.Helpers, path_name, [conn, action])
conn
|> put_req_header("authorization", "#{token(account)}")
|> dispatch(endpoint, method, path, nil)
|> json_response(422)
end
end
当我运行我的规格时,它会返回错误:
** (CompileError) test/support/shared_tests/current_password_required.ex:28: undefined function put_req_header/3
我该如何解决?
答案 0 :(得分:4)
该函数实际上是Plug.Conn.put_req_header/3
- 您需要使用全名或导入Plug.Conn
模块。