I'm testing a GenServer
by unit testing the handle_{call,cast,info}
callbacks. One of my doctests is as follows:
@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"}]}, %{}}
"""
I want to insert the reply to the DB under a specified schema which has been realised in a migration. However I have no solid idea as to how I would write a unit test for this - since the DB write is essentially a side effect, the above doctest would stay the same.
Is it enough to somehow doctest the changeset
independently, then place the Repo.insert
into the GenServer
given a test pass?
As you've mentioned, the piece of functionality you're trying to cover in DocTests has side effects, and this, as due to documentation is discouraged.
You can find more in the "When not to use doctest" section:
In general, doctests are not recommended when your code examples contain side effects...