在控制器测试中使用测试辅助函数
Use test helper function in controller test
当我尝试使用测试辅助函数时出现以下错误:
undefined function myfunction/0
设置如下:
defmodule MyAppWeb.DashboardControllerTest do
use MyAppWebWeb.ConnCase
import TestHelpers
describe "index" do
setup [:myfunction] #<- This works perfectly
test "some test", %{conn: conn} do
conn = get(conn, "/")
var = myfunction #<- This does not work
end
end
end
测试助手看起来像这样:
# File is located in "support/test_helpers.ex"
defmodule TestHelpers do
def myfunction(_) do
#magic!
end
end
如果有人能详细说明就太好了!提前致谢!
编辑 - 完整堆栈跟踪和版本:
== Compilation error in file test/myapp_web/controllers/dashboard_controller_test.exs ==
** (CompileError) test/myapp_web/controllers/dashboard_controller_test.exs:41: undefined function myfunction/0
(elixir 1.11.2) src/elixir_locals.erl:114: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3
(stdlib 3.13.2) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir 1.11.2) lib/kernel/parallel_compiler.ex:416: Kernel.ParallelCompiler.require_file/2
(elixir 1.11.2) lib/kernel/parallel_compiler.ex:316: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
Elixir 1.11.2 (compiled with Erlang/OTP 23)
{:phoenix, "~> 1.5.6"},
我找到了解决方案...我只需要更改
import TestHelpers
到 import MyAppWeb.TestHelpers
.
当我尝试使用测试辅助函数时出现以下错误:
undefined function myfunction/0
设置如下:
defmodule MyAppWeb.DashboardControllerTest do
use MyAppWebWeb.ConnCase
import TestHelpers
describe "index" do
setup [:myfunction] #<- This works perfectly
test "some test", %{conn: conn} do
conn = get(conn, "/")
var = myfunction #<- This does not work
end
end
end
测试助手看起来像这样:
# File is located in "support/test_helpers.ex"
defmodule TestHelpers do
def myfunction(_) do
#magic!
end
end
如果有人能详细说明就太好了!提前致谢!
编辑 - 完整堆栈跟踪和版本:
== Compilation error in file test/myapp_web/controllers/dashboard_controller_test.exs ==
** (CompileError) test/myapp_web/controllers/dashboard_controller_test.exs:41: undefined function myfunction/0
(elixir 1.11.2) src/elixir_locals.erl:114: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3
(stdlib 3.13.2) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir 1.11.2) lib/kernel/parallel_compiler.ex:416: Kernel.ParallelCompiler.require_file/2
(elixir 1.11.2) lib/kernel/parallel_compiler.ex:316: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
Elixir 1.11.2 (compiled with Erlang/OTP 23)
{:phoenix, "~> 1.5.6"},
我找到了解决方案...我只需要更改
import TestHelpers
到 import MyAppWeb.TestHelpers
.