Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Easily capture stdout, stderr at runtime

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Guardfile
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 stdiotrap.gemspec
README.md

StdioTrap Build Status Dependency Status

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

Something went wrong with that request. Please try again.