Skip to content

Instantly share code, notes, and snippets.

@nu7hatch
Created October 17, 2010 21:46
Show Gist options
  • Save nu7hatch/631329 to your computer and use it in GitHub Desktop.
Save nu7hatch/631329 to your computer and use it in GitHub Desktop.
Testing standard input with RSpec
...
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
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
@bradylove
Copy link

This is awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment