Skip to content

Instantly share code, notes, and snippets.

@blakesmith
Created June 18, 2010 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blakesmith/444225 to your computer and use it in GitHub Desktop.
Save blakesmith/444225 to your computer and use it in GitHub Desktop.
def keep_stdout(&block)
begin
orig_stream, $stdout = $stdout, StringIO.new
block.call($stdout)
ensure
s, $stdout = $stdout.string, orig_stream
s
end
end
# Used to test exact STDOUT output
def assert_output(expected, &block)
keep_stdout do |stdout|
block.call
if expected.is_a?(Regexp)
assert_match expected, stdout.string
else
assert_equal expected.to_s, stdout.string
end
end
end
# Used to test if STDOUT contains a certain substring
def assert_output_include?(expected, &block)
keep_stdout do |stdout|
block.call
assert_equal true, stdout.string.include?(expected.to_s)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment