-
-
Save DannyBen/eecb264b8ce6a2fee5d0f03a4b459976 to your computer and use it in GitHub Desktop.
Testing standard input with RSpec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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