gouf
12/12/2014 - 7:38 AM

Testing STDOUT in RSpec 3

Testing STDOUT in RSpec 3

class Mirror
  def echo
    puts 'enter something: '
    response = gets.chomp
    puts "#{response}"
  end
end

# ------------------------

require 'rspec'

describe Mirror do
  let(:solver) { described_class.new }

  before do
    # Set fixed input
    allow(ARGF).to receive(:gets) { "phrase\n" }
  end

  describe 'mirror#echo' do
    it { expect { solver.echo }.to output(/phrase/).to_stdout }
  end
end