次のメソッドで戻り値と IO 出力をテストしたいと思います。
defmodule Speaker do
def speak do
receive do
{ :say, msg } ->
IO.puts(msg)
speak
_other ->
speak # throw away the message
end
end
end
ExUnit.CaptureIO
docsには、次のようなこれを行うサンプル テストがあります。
test "checking the return value and the IO output" do
fun = fn ->
assert Enum.each(["some", "example"], &(IO.puts &1)) == :ok
end
assert capture_io(fun) == "some\nexample\n"
end
spawn
それを考えると、同様のアクションを実行するがed プロセスを使用する次のテストを記述できると思いました。
test ".speak with capture io" do
pid = Kernel.spawn(Speaker, :speak, [])
fun = fn ->
assert send(pid, { :say, "Hello" }) == { :say, "Hello" }
end
assert capture_io(fun) == "Hello\n"
end
ただし、端末に出力が表示されていても、出力がなかったことを示す次のエラー メッセージが表示されます。
1) test .speak with capture io (SpeakerTest)
test/speaker_test.exs:25
Assertion with == failed
code: capture_io(fun) == "Hello\n"
lhs: ""
rhs: "Hello\n"
stacktrace:
test/speaker_test.exs:30: (test)
それで、マクロspawn
を使用する ed プロセスまたはメソッドのテストに関して、おそらく何かが欠けていますか? receive
テストに合格するように変更するにはどうすればよいですか?