Capturing Standard Out In Unit Tests

May 11, 2009 Alex Chaffee

    def capturing_output
      output = StringIO.new
      $stdout = output
      yield
      output.string
    ensure
      $stdout = STDOUT
    end

then…

    it "exits immediately from --version" do
      output = capturing_output do
        lambda {
          Erector.new(["--version"])
        }.should raise_error(SystemExit)
      end
      output.should == Erector::VERSION + "n"
    end

About the Author

Biography

More Content by Alex Chaffee
Previous
Javascript Snacks, Nibble #1: RTE/WYSIWYG is Built in to your Browser
Javascript Snacks, Nibble #1: RTE/WYSIWYG is Built in to your Browser

The Label "Tasty! No Trans-Fats." I've now worked with a number clients who have a taste for a "ric...

Next
Refactoring a dead horse
Refactoring a dead horse

A while back I made the point that the HasOneThroughAssociation class in Rails shouldn't be a subclass of H...

Enter curious. Exit smarter.

Learn More