Skip to content

Instantly share code, notes, and snippets.

@devver
Created July 18, 2009 19:18
Show Gist options
  • Save devver/149667 to your computer and use it in GitHub Desktop.
Save devver/149667 to your computer and use it in GitHub Desktop.
class Test::Unit::TestCase
def capture_io
require 'stringio'
orig_stdout, orig_stderr = $stdout, $stderr
captured_stdout, captured_stderr = StringIO.new, StringIO.new
$stdout, $stderr = captured_stdout, captured_stderr
yield
return captured_stdout.string, captured_stderr.string
ensure
$stdout = orig_stdout
$stderr = orig_stderr
end
end
class FooTest < Test::Unit::TestCase
def test_io_sample
stdout, stderr = capture_io do
puts "x"
end
assert_equal "x\n", stdout
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment