Project

stdiotrap

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Easily capture stdout, stderr at runtime
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

StdioTrap Build Status Gem Version

StdioTrap lets you capture stdout, stderr at runtime, e.g. for inspection inside a unit-test. It can also fake stdin.

Example (rspec)

require 'stdiotrap'

describe StdioTrap do

  describe 'Inline usage' do
    it "captures stdout and stderr" do
      trapped = StdioTrap.capture {
        puts "Hello world!"
        $stderr.puts "Hello other world!"
      }
      trapped[:stdout].should == "Hello world!\n"
      trapped[:stderr].should == "Hello other world!\n"
    end
  end

  describe 'Common rspec usage' do
    before :each do
      StdioTrap.trap!
    end

    after :each do
      StdioTrap.release!
    end

    it "captures stdout and stderr" do
      puts "Hello world!"
      $stderr.puts "Hello other world!"
      StdioTrap.stdout.should == "Hello world!\n"
      StdioTrap.stderr.should == "Hello other world!\n"
    end
  end

end

For more usage examples please refer to the spec suite.

Credits

StdioTrap is based on this code snippet (bottom of page) by Ng Tze Yang