On Oct 24, 8:42 am, Ryan Davis <ryand-r... / zenspider.com> wrote:
> On Oct 24, 2007, at 01:30 , Feng Tien wrote:
>
> > How do I write an assertion so it tests if, say the last line
> > printed is
> > equal to the expected? Lasts just say the program asks for 2 numbers,
> > and outputs all the number from the lowest to the highest.
>
> The easiest way to do it is to store off stdout and replace it with a
> StringIO and test against that. Replace stdout in teardown.

Simple example:

require 'test/unit'
require 'stringio'

class Blah
  def do_something
    puts "Done"
  end
end

class TestBlah < Test::Unit::TestCase
  def setup
    @save_stdout = $stdout
    $stdout = StringIO.new
  end

  def teardown
    $stdout = @save_stdout
  end

  def test_do_something
    b = Blah.new
    b.do_something
    assert_equal "Done\n", $stdout.string
  end
end