Search code examples
rubyunit-testingruby-1.9.3stringio

Best way to mock out StringIO input for Ruby unit test?


Is there a better way to mock out StringIO input? I'm specifically looking for a way to avoid having to call the .rewind method. I tried using the block form of StringIO.open but it zeros out the internal string when returning it.

test_input = StringIO.new
test_input.puts '### Functional Tags'
test_input.puts '"@api"   *Workflow API*'
test_input.puts '"@categorize"'
test_input.puts ''
test_input.puts '"@combine"'
test_input.puts '"@flaky"  *delicate tests*'
test_input.rewind

Solution

  • How about:

    test_input = StringIO.new <<-RUBY
    ### Functional Tags'
    "@api"   *Workflow API*
    "@categorize"
    
    "@combine"
    "@flaky"  *delicate tests*
    RUBY