title: Testing recursive IO prompt in Elixir/Erlang
body: <p>I have a function <code>confirm</code>, which reads IO input, and depending by input, if it's <code>y</code> (yes) or <code>n</code> (no), it returns <code>true/false</code>, otherwise it calls <code>confirm</code> again, until any expected <code>y</code> or <code>n</code> is entered.</p>
  
  <pre><code>@spec confirm(binary) :: boolean
  def confirm(question) do
    answer = question |&gt; IO.gets() |&gt; String.trim() |&gt; String.downcase()
  
    case(answer) do
      "y" -&gt; true
      "n" -&gt; false
      _ -&gt; confirm(question)
    end
  end
  </code></pre>
  
  <p>To test <code>y</code> and <code>n</code> cases it's easy:</p>
  
  <p><code>assert capture_io([input: "y"], fn -&gt; confirm("Question") end) == "Question"</code></p>
  
  <p>But I have no idea how to capture IO multiple times to test recursive case, let's say if at first input is "invalid" and then "y". Does elixir has any way to test IO functions like this? Or maybe do you have some suggestions how I could rewrite function to test it easier?</p>
  
  <p>Original question <a href="https://elixirforum.com/t/testing-recursive-io-prompt/3715" rel="nofollow noreferrer">https://elixirforum.com/t/testing-recursive-io-prompt/3715</a></p>
  
  <p>Thanks for the help.</p>