Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何在不调用应用程序回调的情况下运行单元测试_Unit Testing_Elixir - Fatal编程技术网

Unit testing 如何在不调用应用程序回调的情况下运行单元测试

Unit testing 如何在不调用应用程序回调的情况下运行单元测试,unit-testing,elixir,Unit Testing,Elixir,我的应用程序回调启动管理器与单元测试冲突 当我试图运行单元测试时,由于我的进程已经启动,我得到了类似于{:error,{:ready_start,#PID}}的结果 我能否仅对:dev和:prod执行应用程序回调,保持:test环境中没有启动代码 我在找这样的东西: def application do [ applications: [:logger], mod: {MyApplication, [], only: [:dev, :prod]} ] only:[:dev,:prod

我的应用程序回调启动管理器与单元测试冲突

当我试图运行单元测试时,由于我的进程已经启动,我得到了类似于
{:error,{:ready_start,#PID}}
的结果

我能否仅对
:dev
:prod
执行应用程序回调,保持
:test
环境中没有启动代码

我在找这样的东西:

def application do
[
  applications: [:logger],
  mod: {MyApplication, [], only: [:dev, :prod]} 
]

only:[:dev,:prod]
-这是缺少的部分

一个解决方案是在测试套件运行之前终止该进程。例如,您可以执行以下操作:

setup do
  Process.exit(pid, :kill)
end

test "do something..." do
  assert 1 == 1
end

这将确保在运行测试之前,该进程已被终止。

我不知道在这种情况下,这是否是处理测试的正确方法,但以下是如何满足您的要求:

mix.exs
中:

def application do
  rest = if(Mix.env == :test, do: [], else: [mod: {MyApp, []}])
  [applications: [:logger]] ++ rest
end
对于下面的演示,我将以下内容添加到了
MyApp.start/2

IO.puts "starting app..."
演示:


你的意思是只说:[:dev,:prod]?@Dogbert你说得对。将解决这个问题。这肯定会起作用,但我希望避免使用清理代码设置每个测试。谢谢你的回复。
$ MIX_ENV=dev mix
starting app...
$ MIX_ENV=prod mix
starting app...
$ MIX_ENV=test mix # no output