【发布时间】:2020-12-27 04:15:46
【问题描述】:
我目前正在处理我的第一个大型 Elixir 项目,并且希望这次能够适当地利用测试。
但是,如果我将我的模块添加到“普通”主管,我无法使用start_supervised! 再次启动它们,并且所有测试都以Reason: already started: #PID<0.144.0> 失败
这是我的代码:
(应用程序.ex)
defmodule Websocks.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
def start(_type, _args) do
children = [
{Websocks.PoolSupervisor, []},
{Websocks.PoolHandler, %{}}
# {Websocks.Worker, arg}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Websocks.Supervisor]
Supervisor.start_link(children, opts)
end
end
我的一些测试:
defmodule PoolHandlerTest do
use ExUnit.Case, async: true
alias Websocks.PoolHandler
doctest PoolHandler
setup do
start_supervised!({PoolHandler, %{}})
%{}
end
test "adding two pools and checking if they are there" do
assert PoolHandler.add(:first) == :ok
assert PoolHandler.add(:second) == :ok
assert PoolHandler.get_pools() == {:ok,%{:first => nil, :second => nil}}
end
和池处理程序:
defmodule Websocks.PoolHandler do
use GenServer
# Client
def start_link(default) when is_map(default) do
GenServer.start_link(__MODULE__, default, name: __MODULE__)
end
# Server (callbacks)
@impl true
def init(arg) do
{:ok, arg}
end
end
(我认为不需要的东西我删掉了,但完整的代码在github上:github)
提前感谢我得到的任何帮助!
【问题讨论】:
-
我认为您不需要为
application.ex中列出的测试开始工作——据我所知,application.ex中的所有内容都是在您进行测试运行时开始的。