Excerpts from Chuck Remes's message of Wed Jan 12 07:52:58 -0800 2011:
> I'd like to bump this message because I am facing a similar situation. 
> 
> What's a good technique for spec'ing code that prints to STDOUT yet keeps the 
> spec output nice and clean?

I can't promise that what follows is a “good” technique for this, but it works
for me.  At the moment, I can't quite recall why this doesn't get in the way of
rspec's own output.

The stuff outside of the configure block is not strictly necessary, but helpful
if you want to be able to debug specs without the output of ruby-debug being
sent to your buffers.

  # spec/spec_helper.rb

  Rspec.configure do |c|
    # capture output
    @output         = ''
    @err            = ''
    $stdout.stub!( :write ) { |*args| @output.<<( *args )}
    $stderr.stub!( :write ) { |*args| @err.<<( *args )}
  end

  # Track original $stdout, $stderr write methods so we can restore them for
  # debugging

  class << $stdout
    alias_method :real_write, :write
  end
  class << $stderr
    alias_method :real_write, :write
  end


  class Object
    def debug
      # For debugging, restore stubbed write
      class << $stdout
        alias_method :write, :real_write
      end
      class << $stderr
        alias_method :write, :real_write
      end

      require 'ruby-debug'
      debugger
    end
  end


-- 
med vänlig hälsning
David J. Hamilton
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to