Alex ChaffeeAlex Chaffee
Capturing Standard Out In Unit Tests
edit Posted by Alex Chaffee on Monday May 11, 2009 at 03:28AM
    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

Comments

  1. Tim Connor Tim Connor on May 13, 2009 at 04:12PM

    You grab that from the rake tests, as well? I was doing the same thing for rake and that is where I found it.

  2. Ian Smith-Heisters Ian Smith-Heisters on May 14, 2009 at 12:56AM

    I prefer doing it with Open3: http://gist.github.com/100773

  3. TJ Holowaychuk TJ Holowaychuk on May 15, 2009 at 05:22AM

    Use commander for executables :D https://visionmedia.github.com/commander