Enum.each(&IO.puts(&1)) one two three :ok 我的测试目前看起来像这样,但失败了,因为-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:04:09 25 4
gpt4 key购买 nike

如何测试以下代码?

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

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

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

也许IO.puts对于这个用例来说是错误的函数。如果是这样,我可以使用什么替代方案?

提前致谢。

最佳答案

使用capture_io

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

关于elixir - 如何在 Elixir 中测试多行输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40369041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com