使用 TDD 的 Ecto 变更集和 GenServers
Ecto changesets and GenServers using TDD
我正在测试 unit testing the handle_{call,cast,info}
callbacks 的 GenServer
。我的一个doctest如下:
@doc """
Call the GenServer to retrieve the initial workout
## Examples
iex> :rand.seed(:exsplus, {101, 102, 103})
iex> Pullapi.GenServerWorker.handle_call({:initial_workout, 5}, nil, %{})
{:reply,
{:ok,
[%{"Action" => "Pullups", "SequenceNo" => 0, "Units" => "1"},
%{"Action" => "Rest", "SequenceNo" => 1, "Units" => "60"},
%{"Action" => "Pullups", "SequenceNo" => 2, "Units" => "3"},
%{"Action" => "Rest", "SequenceNo" => 3, "Units" => "60"},
%{"Action" => "Pullups", "SequenceNo" => 4, "Units" => "3"},
%{"Action" => "Rest", "SequenceNo" => 5, "Units" => "70"}]}, %{}}
"""
我想在迁移中实现的指定模式下插入对数据库的回复。但是我对于如何为此编写单元测试没有明确的想法 - 因为数据库写入本质上是一个副作用,上面的 doctest 将保持不变。
是否足以以某种方式 doctest changeset
独立,然后将 Repo.insert
放入 GenServer
给定测试通过?
正如您所提到的,您试图在 DocTests 中涵盖的功能有副作用,并且由于文档原因,不鼓励这样做。
您可以在 "When not to use doctest" section 中找到更多信息:
In general, doctests are not recommended when your code examples contain side effects...
我正在测试 unit testing the handle_{call,cast,info}
callbacks 的 GenServer
。我的一个doctest如下:
@doc """
Call the GenServer to retrieve the initial workout
## Examples
iex> :rand.seed(:exsplus, {101, 102, 103})
iex> Pullapi.GenServerWorker.handle_call({:initial_workout, 5}, nil, %{})
{:reply,
{:ok,
[%{"Action" => "Pullups", "SequenceNo" => 0, "Units" => "1"},
%{"Action" => "Rest", "SequenceNo" => 1, "Units" => "60"},
%{"Action" => "Pullups", "SequenceNo" => 2, "Units" => "3"},
%{"Action" => "Rest", "SequenceNo" => 3, "Units" => "60"},
%{"Action" => "Pullups", "SequenceNo" => 4, "Units" => "3"},
%{"Action" => "Rest", "SequenceNo" => 5, "Units" => "70"}]}, %{}}
"""
我想在迁移中实现的指定模式下插入对数据库的回复。但是我对于如何为此编写单元测试没有明确的想法 - 因为数据库写入本质上是一个副作用,上面的 doctest 将保持不变。
是否足以以某种方式 doctest changeset
独立,然后将 Repo.insert
放入 GenServer
给定测试通过?
正如您所提到的,您试图在 DocTests 中涵盖的功能有副作用,并且由于文档原因,不鼓励这样做。
您可以在 "When not to use doctest" section 中找到更多信息:
In general, doctests are not recommended when your code examples contain side effects...