Sha256: 8f75bd5e4546d55b6b5db462b439b8fdafeeb511361d5b4548b662ebe50030b5

Contents?: true

Size: 1.97 KB

Versions: 5

Compression:

Stored size: 1.97 KB

Contents

require 'spec_helper'
require 'gorillib/utils/capture_output'

describe Gorillib::TestHelpers, :simple_spec => true do

  context '.capture_output' do
    let(:mock_stdout){ StringIO.new('', 'w') }
    let(:mock_stderr){ StringIO.new('', 'w') }

    before do
      mock_stderr ; mock_stdout
      StringIO.should_receive(:new).and_return(mock_stdout)
      StringIO.should_receive(:new).and_return(mock_stderr)
    end

    it 'gives me a new $stdout and $stderr' do
      Gorillib::TestHelpers.capture_output do
        $stdout.should equal(mock_stdout)
        $stderr.should equal(mock_stderr)
      end
    end

    it 'yields a wrapped block' do
      i_wuz_here = nil
      Gorillib::TestHelpers.capture_output do
        i_wuz_here = true
      end
      i_wuz_here.should == true
    end

    it 'restores the original values' do
      old_stdout = $stdout ; old_stderr = $stderr
      Gorillib::TestHelpers.capture_output do
        $stdout.should equal(mock_stdout)
        $stderr.should equal(mock_stderr)
      end
      $stdout.should equal(old_stdout)
      $stderr.should equal(old_stderr)
    end

    it 'restores the original values even if there is an error' do
      old_stdout = $stdout ; old_stderr = $stderr
      Gorillib::TestHelpers.capture_output do
        raise 'hell'
      end rescue nil
      $stdout.should equal(old_stdout)
      $stderr.should equal(old_stderr)
    end

    it 'returns the captured streams' do
      returned_stdout, returned_stderr = Gorillib::TestHelpers.capture_output do
        $stdout.puts 'I was here'
        $stderr.puts 'so was I'
      end
      returned_stdout.should equal(mock_stdout)
      returned_stderr.should equal(mock_stderr)
      mock_stdout.string.should == "I was here\n"
      mock_stderr.string.should == "so was I\n"
    end

    it 'raises an error if no block is given' do
      lambda{ Gorillib::TestHelpers.capture_output }.should raise_error(LocalJumpError, /no block given/)
    end

  end

  context '.quiet_output' do

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gorillib-0.6.0 spec/gorillib/utils/capture_output_spec.rb
gorillib-0.5.2 spec/gorillib/utils/capture_output_spec.rb
gorillib-0.5.0 spec/gorillib/utils/capture_output_spec.rb
gorillib-0.4.2 spec/gorillib/utils/capture_output_spec.rb
gorillib-0.4.2pre spec/gorillib/utils/capture_output_spec.rb