Capturing output with UnitTest in Ruby

Problem

You have some ruby code, a rake task for example, that outputs some results in the standard output, but you would like to test it in your unit tests.
MiniTest has capture_io and capture_subprocess_io, but there is nothing similar in UnitTest.

Solution

Looking at the code of the above MiniTest assertions you can create your own assertion to be used in your tests.

Create a new file under your test folder called MyAssertions and use the following code (taken from MiniTest), for your capture_output assertion:

module MyAssertions
  module CaptureOutput
    # Use as:
    # out = capture_output { method_to_call(param1, param2) }
    # output = out[0], error = out[1] or
    # output = out.first, error = out.last
    def capture_output
      require 'stringio'

      captured_stdout, captured_stderr = StringIO.new, StringIO.new
      orig_stdout, orig_stderr = $stdout, $stderr
      $stdout, $stderr         =  captured_stdout, captured_stderr

      begin
        yield
      ensure
        $stdout = orig_stdout
        $stderr = orig_stderr
      end
      return captured_stdout.string, captured_stderr.string
    end

    def capture_subprocess_output
      require 'tempfile'

      captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err")
      orig_stdout, orig_stderr = $stdout.dup, $stderr.dup
      $stdout.reopen captured_stdout
      $stderr.reopen captured_stderr

      begin
        yield

        $stdout.rewind
        $stderr.rewind

        [captured_stdout.read, captured_stderr.read]
      ensure
        captured_stdout.unlink
        captured_stderr.unlink
        $stdout.reopen orig_stdout
        $stdout.reopen orig_stderr
      end
    end
  end

  include CaptureOutput
end 

Then include it in your test_helper.rb file with:

require './test/my_assertions.rb'

and use it in your tests like:

out = capture_output { method_called(prm1, prm2) }

assert_match(/required_value/, out.first)