如何在不同于lib(Elixir)的路径中加载文件

Elo*_*nco 5 testing elixir

我试图在测试中使用帮助程序执行任务.我的文件夹结构是这样的:

config/
lib/
test/
test/test_helper.exs
test/provider_test/(the test files are here)
Run Code Online (Sandbox Code Playgroud)

而现在我想做的就是这个

config/
lib/
test/
test/test_helper.exs
test/provider_test/(the test files are here)
test/provider_test/helpers/(the helpers files... more or less, one for helper)
Run Code Online (Sandbox Code Playgroud)

我尝试使用它(在测试中):

HelperModuler.calling_function(args)
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

 (UndefinedFunctionError) undefined function HelperModuler.calling_function/1 (module HelperModuler is not available)
Run Code Online (Sandbox Code Playgroud)

Dog*_*ert 10

要在所有测试中使模块可用,您需要做两件事:

  1. 将它放在带扩展名的文件中.ex.
  2. 将包含该文件的文件夹添加到elixirc_paths从中返回的值的键MyApp.Mixfile.project/0mix.exs.

例如,以下是Phoenix如何处理test/support仅在:test混合环境中添加mix.exs:

def project do
  [...,
   elixirc_paths: elixirc_paths(Mix.env),
   ...]
end

# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "web", "test/support"]
defp elixirc_paths(_),     do: ["lib", "web"]
Run Code Online (Sandbox Code Playgroud)


Fre*_*Dog 4

您还可以在运行时加载代码以Code.require_file在 test.helpers 设置中使用测试。

 Code.require_file("test/provider_test/helpers/helper.exs")
Run Code Online (Sandbox Code Playgroud)