require 'stringio'
def capture_stdout
out = StringIO.new
$stdout = out
yield
return out
ensure
$stdout = STDOUT
end
Which can be used as follows
class ATest < Test::Unit::TestCase
def zxc(text, count)
count.times {puts text.upcase}
end
def test_stdout_capture
out = capture_stdout do
zxc('yes', 3)
end
assert_equal "YES\nYES\nYES\n", out.string
end
end
No comments:
Post a Comment