Created
October 17, 2010 21:46
-
-
Save nu7hatch/631329 to your computer and use it in GitHub Desktop.
Testing standard input with RSpec
This file contains 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 fake_stdin(*args) | |
begin | |
$stdin = StringIO.new | |
$stdin.puts(args.shift) until args.empty? | |
$stdin.rewind | |
yield | |
ensure | |
$stdin = STDIN | |
end | |
end | |
end | |
... | |
RSpec.configure do |conf| | |
conf.include(Helpers) | |
end |
This file contains 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 | |
fake_stdin("foobar") do | |
input = gets.to_s.chomp.strip | |
input.should == "foobar" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome, thank you!