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

时间:2016-08-25 13:12:51

标签: testing elixir

我试图在测试中使用帮助程序来执行任务。我的文件夹结构如下:

config/
lib/
test/
test/test_helper.exs
test/provider_test/(the test files are here)

现在我要做的就是这个

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)

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

HelperModuler.calling_function(args)

但是我收到了这个错误:

 (UndefinedFunctionError) undefined function HelperModuler.calling_function/1 (module HelperModuler is not available)

3 个答案:

答案 0 :(得分:10)

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

  1. 将其放入扩展名为.ex
  2. 的文件中
  3. 将包含该文件的文件夹添加到elixirc_pathsMyApp.Mixfile.project/0返回值的mix.exs键。
  4. 例如,以下是凤凰城处理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"]
    

答案 1 :(得分:4)

您还可以在test.helpers设置中使用 Code.require_file("test/provider_test/helpers/helper.exs") 在运行时加载代码以进行测试。

$scope.pickFile = function() {
    filepicker.setKey("...");
    filepicker.pick(
        {},
        function(Blob) {  // OnSuccess
            var uuid = Blob.url.split('/').slice(-1)[0];
            window.location.replace("/url" + uuid);
            $.post(
                "/other_url" + uuid,
                {'input': $('#rawText')[0].checked? "text" : "features"}
            );
    });
}

答案 2 :(得分:1)

def project do
[app: :example,
 version: "0.1.0",
 elixir: "~> 1.4",
 build_embedded: Mix.env == :prod,
 start_permanent: Mix.env == :prod,
 elixirc_paths: ["lib", "path_to_your_src"],
 deps: deps()]
end

将此行elixirc_paths: ["lib", "path_to_your_src"],添加到project

mix.exs函数的返回内容