当我在项目上运行mix test
时,出现以下错误,我不明白为什么:
$ mix test
Compiling 2 files (.ex)
== Compilation error in file lib/myproject_web/controllers/email_controller_test.ex ==
** (RuntimeError) cannot use ExUnit.Case without starting the ExUnit application, please call ExUnit.start() or explicitly start the :ex_unit app
expanding macro: ExUnit.Case.__using__/1
lib/myproject_web/controllers/email_controller_test.ex:2: MyProjectWeb.EmailControllerTest (module)
(elixir 1.10.1) expanding macro: Kernel.use/2
lib/myproject_web/controllers/email_controller_test.ex:2: MyProjectWeb.EmailControllerTest (module)
我在test_helper.exs
中已经有一个lib/
文件,它调用ExUnit.start()
。我的设置很不正常,因为我希望测试位于模块旁边,而不是在单独的test
文件夹中。
事实证明,此错误来自扩展名为.ex
而不是.exs
的测试文件。使用.ex
,然后mix
会尝试在运行测试之前将其与所有其他内容一起编译,然后抱怨,因为在编译时尚未调用ExUnit.start()
。
如果您阅读mix test
的文档: