【问题标题】:How load files in a path different than lib (Elixir)如何在不同于 lib (Elixir) 的路径中加载文件
【发布时间】:2017-07-25 11:11:08
【问题描述】:

我试图在测试中使用帮助器来完成任务。我的文件夹结构是这样的:

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)

【问题讨论】:

    标签: testing elixir


    【解决方案1】:

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

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

    例如,Phoenix 如何为 :test Mix 环境添加 test/support 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"]
    

    【讨论】:

      【解决方案2】:

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

       Code.require_file("test/provider_test/helpers/helper.exs")
      

      【讨论】:

        【解决方案3】:
        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"], 添加到mix.exsproject 函数的返回

        【讨论】:

          猜你喜欢
          • 2022-08-02
          • 2019-01-30
          • 1970-01-01
          • 2021-01-11
          • 1970-01-01
          • 2020-01-15
          • 1970-01-01
          • 2015-11-21
          • 1970-01-01
          相关资源
          最近更新 更多