From: Nathaniel Talbott Date: 2004-04-03T04:31:56+09:00 Subject: Re: [newbie] assert_writes implementation On Apr 2, 2004, at 13:06, Alexey Verkhovsky wrote: > Context: I needed to teach a subclass of Test::Unit::TestCase to > assert that a particular block writes something to an output stream. > > In Java, an easy way to do something like this is with a pair of > PipedOutputStream/PipedInputStream pair. So I tried the same thing in > Ruby, and the result looks bit hairy to me. Having to use > multi-process semantics to write top a buffer and then read from it is > a bit over the top. There must be some better way. Would StringIO help? (http://www.ruby-doc.org/stdlib/libdoc/stringio/rdoc/index.html) require 'test/unit' require 'stringio' class TestAssertWrite < Test::Unit::TestCase def assert_write(expected) io = StringIO.new yield(io) assert_equal(expected, io.string) end def test_assert_write assert_raise(Test::Unit::AssertionFailedError) do assert_write("expected") do |output| output.write("bogus") end end assert_nothing_raised do assert_write("expected") do |output| output.write("expected") end end end end Nathaniel Terralien, Inc. <:((><