简体   繁体   中英

Elixir check if a function has been called from a ExUnit test?

Is there a way to check if a function has been called from a ExUnit test case? I have a function that calls IO.puts and I would like to test and verify that something is being outputted to STDOUT via a test case.

I saw this: http://erlang.org/pipermail/erlang-questions/2005-July/016259.html . But I was hoping there might be a simpler way of doing this with ExUnit.

Is there a way to check and see if my function calls IO.puts? Or at least check and see if something has been sent to STDOUT?

If you just want to test the STDOUT via a test case, I think CaptureIO can be used. The following has the spec and sample code.

https://hexdocs.pm/ex_unit/master/ExUnit.CaptureIO.html

You could mock the method calls using some mocking libraries, but for STDOUT, the above one would be simpler approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM