Module: Test::Unit::Assertions

Defined in:
lib/csd/vendor/zentest/zentest_assertions.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#captureObject

Captures $stdout and $stderr to StringIO objects and returns them. Restores $stdout and $stderr when done.

Usage:

def test_puts
  out, err = capture do
    puts 'hi'
    STDERR.puts 'bye!'
  end
  assert_equal "hi\n", out.string
  assert_equal "bye!\n", err.string
end


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/csd/vendor/zentest/zentest_assertions.rb', line 47

def capture
  require 'stringio'
  orig_stdout = $stdout.dup
  orig_stderr = $stderr.dup
  captured_stdout = StringIO.new
  captured_stderr = StringIO.new
  $stdout = captured_stdout
  $stderr = captured_stderr
  yield
  captured_stdout.rewind
  captured_stderr.rewind
  return captured_stdout.string, captured_stderr.string
ensure
  $stdout = orig_stdout
  $stderr = orig_stderr
end