Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

busyloop/stdiotrap

Repository files navigation

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

About

Easily capture stdout, stderr at runtime

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages