Description
It is common for Elixir code to output something to the CLI. Those, however, become non-trivial to test since we don't provide a mechanism to capture IO.
Luckily, all the IO in Erlang is done via the IO protocol, which means we can replace the Erlang group leader and capture the IO. This will make it possible to test many warnings in Elixir, test IEx.Helpers and more.
This is a proposal for adding a function called capture_io/1
to ExUnit.Assertions
(or maybe ExUnit.Helpers
) that receives a function and while it executes the function, all the output is captured (and the input is mocked to return nothing so far):
assert capture_io(fn ->
Code.eval "some code that emits a warning"
end) =~ %r/got warning/
To implement capture_io/1
, we need to take a better look at the "IO protocol". Luckily, the eunit test framework in Erlang implements it too, so we can always take a look at it for clues.