From: "Skye Shaw!@#$" Date: 2007-08-08T16:04:57+09:00 Subject: Re: capturing stdout during unit tests On Aug 7, 8:59 pm, Mark Slater wrote: > I've written a script for use with Nagios (a monitoring tool) that > I'd like to test. > I'm now writing unit tests for my script, and I'd like to check that > the output written to STDOUT by each service monitor class is > correct. But I'm new to Ruby and I'm not sure how to do that. In > other languages, I'd simply redefine STDOUT as a stream that goes to > a large in-memory buffer, What language is this in? > but I haven't seen anything that suggests that is possible in Ruby. At any rate, Ruby does have similar functionality. An example: require 'stringio' class Bs attr :output_stream,true def do_some_output(data) output_stream.puts(data) end end bs=Bs.new bs.output_stream=$stdout bs.do_some_output("this is to stdout") expect="grrrrrrrr" result=String.new bs.output_stream=StringIO.new(result,"w+") bs.do_some_output("Waka waka waka!") bs.output_stream.close puts "result= " + result puts "expect= " + expect [sshaw@localhost sshaw]# ruby bs.rb this is to stdout result= Waka waka waka! expect= grrrrrrrr