require 'stringio' class Hello def hello puts "hello" end end def capture(stream) begin stream = stream.to_s eval "$#{stream} = StringIO.new" yield result = eval("$#{stream}").string ensure eval "$#{stream} = #{stream.upcase}" end result end describe "Hello#helloは'hello'と標準出力すること" @target = Hello.new capture(:stdout) { @target.hello }.should == 'hello\n' end