Hi Guys, this is my fist post, and first delve into the elixir language. I am in process of reading Dave Thomas’ book and was doing the exercises like a real nice boy would, and I got interested in Elixir’s doctest feature (which is not in the book’s chapter I am in, btw). But I ran into the following issue:
Some of my methods, just print stuff to the console, IO.puts. When I wrote regular tests, on the test file, using ExUnit.{Case, CaptureIO}, I would simply do something like:
test "Basic game output" do
assert capture_io(fn ->
guess(273, 1..1000)
end) ==
"Is it 500?\nIs it 250?\nIs it 375?\nIs it 312?\nIs it 281?\nIs it 265?\nIs it 273?\n273\n"
end
So my first question, is: How could I doctest a function that does an IO.puts, instead of actually returning a value?
Perhaps the answer will be: use both styles, doctest for methods that return values and ExUnit.CaptureIO for methods that print to the console. What is the take on this?
Another question would be: how can I test methods that may result in System.halt/1 ? This was breaking my test flow. What I have done so far, is extracted a my_halt/1
method that simply calls System.halt(code)
when env is not :test