Skip to content

Instantly share code, notes, and snippets.

@DannyBen
Forked from nu7hatch/spec_helper.rb
Last active February 15, 2018 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DannyBen/eecb264b8ce6a2fee5d0f03a4b459976 to your computer and use it in GitHub Desktop.
Save DannyBen/eecb264b8ce6a2fee5d0f03a4b459976 to your computer and use it in GitHub Desktop.
Testing standard input with RSpec
module Helpers
# Replace standard input with faked one StringIO.
def stdin_send(*args)
begin
$stdin = StringIO.new
$stdin.puts(args.shift) until args.empty?
$stdin.rewind
yield
ensure
$stdin = STDIN
end
end
end
RSpec.configure do |c|
c.include(Helpers)
end
require "spec_helper"
describe "standard input" do
it "should receive `foobar`" do
stdin_send("foobar") do
input = gets.to_s.chomp.strip
expect(input).to eq "foobar"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment