On Sat, Aug 17, 2002 at 04:41:43AM +0900, Gray, Jeff wrote:
> I'm sorta new to the whole unit testing thing, and my only prior exposure to
> software testing is using DejaGnu/Expect.  I'm just starting to use
> Test::Unit, and I have a case where a certain action should produce a
> warning message.  There is no other side effect that can be tested to make
> sure the condition that produced the warning was indeed exercised properly,
> just the appearance of the message.
> 
> I don't see any obvious assertion in Test::Unit that allows checking of I/O
> to $stdout/$stderr.  What's a good way to test for such a case?


Get stringio.rb from www.allruby.com/rpkg/stringio.rb


require 'stringio.rb'

class TestSomething < Test::Unit::TestCase
  def set_up 
    $stdin, @out, @err = StringIO.new, StringIO.new, StringIO.new
  end

  def test_something
    redir(@out) { say_hello }

    assert_equal "hello world\n", @out.to_s
  end

  # you can test input as well

  def test_input
    $stdin.puts "user_command"
    $stdin.rewind
    redir(@out) { command_that_acts_based_on_stdin }

    assert_equal "was issued: <user_command>\n", @out.to_s    
  end
end


Massimiliano