If you write console based applications in ruby, chances are you’re going to want to get some test coverage on that eventually.
StringIO is a great class to use when you want to assert that your application outputs the correct stuff to the screen.
We can modify the global variable
$stdout to be an instance of
StringIO for the duration of our tests. Any method that outputs text on
require 'stringio' describe StringIO do before do $stdout = StringIO.new end after do # always clean up after yourself! $stdout = STDOUT end it "should help capture standard output" do puts "foo" puts "bar" $stdout.string.should == "foo\nbar\n" end end
Not a bad bit of TDD if I don’t say so!
Similar tricks could be used with
$stdin to get solid end-to-end test coverage on a wide variety of console-based applications.