Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Tools
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Capturing Standard Out In Unit Tests

Alex Chaffee
Monday, May 11, 2009
    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
  • 0 Shares
  • Share on Facebook
  • Share on Twitter

3 Comments

  1. Tim Connor says:

    You grab that from the rake tests, as well? I was doing the [same thing for rake](http://www.timocracy.com/articles/2008/02/21/calling-invoking-rails-rake-tasks-from-within-ruby-for-testing-try-2) and that is where I found it.

    May 13, 2009 at 4:12 pm

  2. Ian Smith-Heisters says:

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

    May 14, 2009 at 12:56 am

  3. TJ Holowaychuk says:

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

    May 15, 2009 at 5:22 am

Add New Comment Cancel reply

Your email address will not be published.

Alex Chaffee

Recent Posts

  • Code Monkey
  • RubyGems Warningitis Outbreak
  • Upgrading your Rakefile from RSpec 1.3 to RSpec 2
Subscribe to Alex's Feed

Author Topics

ruby (14)
gem (5)
ruby on rails (24)
erector (2)
rspec (4)
activerecord (4)
database (3)
sinatra (1)
postgresql (1)
css (1)
html (1)
git (2)
mac (5)
java (3)
agile (12)
iphone (1)
migrations (1)
fun (5)
dot (1)
graphing (1)
subversion (1)
test (1)
demeter (1)
puzzler (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Tools
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >