My second test is failing.
1) test it jumps when it can (RunnerTest)
test/runner_test.exs:15
No message matching %{y: 1} after 100ms.
The process mailbox is empty.
stacktrace:
test/runner_test.exs:18: (test)
defmodule RunnerTest do
use ExUnit.Case
doctest Runner
setup_all do
{:ok, pid: spawn(fn -> Runner.input() end)}
end
test "it increases its x position", state do
avatar = %{x: 0, y: 0}
send state[:pid], {:run, self, avatar}
assert_receive %{x: 1}
end
test "it jumps when it can", state do
avatar = %{x: 0, y: 0}
send state[:pid], {:jump, self, avatar}
assert_receive %{y: 1}
end
end
Runner Module:
defmodule Runner do
def input do
receive do
{:run, sender, mover} -> send sender, run(mover)
{:jump, sender, mover} -> send sender, jump(mover)
end
end
defp run(mover) do
Map.merge(mover, %{x: mover.x + 1})
end
defp jump(mover) do
case {mover.y} do
{0} ->
Map.merge(mover, %{y: mover.y + 1})
_ ->
mover
end
end
end
This test passes:
test "it jumps when it can", state do
avatar = %{x: 0, y: 0}
pid = spawn(fn -> Runner.input() end)
send pid, {:jump, self, avatar}
assert_receive %{y: 1}
end
Why is 1 test (run) working with setup but not the other (jump)?
Why is 1 test (run) working with setup but not the other (jump)?
This is because your process only responds to one message and then dies and you're using setup_all
instead of setup
, which means the process is started only once, before the first test is ran, and the same pid
is passed to all tests.
You can either change setup_all
to setup
if you want to spawn a fresh process for each test, or make your process respond to more than 1 message (possibly infinite using self recursion).