如何在 Elixir 中测试多行输出?

Fel*_*tis 2 stdout elixir ex-unit

如何测试以下代码?

["one", "two", "three"]) |> Enum.each(&IO.puts(&1))
one
two
three
:ok
Run Code Online (Sandbox Code Playgroud)

我的测试目前看起来像这样,但失败了,因为IO.puts返回:ok而不是字符串,并且可能不包括完整字符串中的换行符。

assert ["one", "two", "three"]) |> Enum.each(&IO.puts(&1)) == """
one
two
three
"""
Run Code Online (Sandbox Code Playgroud)

也许IO.puts是这个用例的错误功能。如果是这样,我可以使用什么替代方案?

提前致谢。

Mar*_*lin 5

使用capture_io.

fun = fn -> ["one", "two", "three"] |> Enum.each(&IO.puts/1) end
assert capture_io(fun) == "one\ntwo\nthree\n"
Run Code Online (Sandbox Code Playgroud)