Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Forked from jdunphy/input-test-two.rb
Created November 11, 2009 00:15
Show Gist options
  • Save tenderlove/231428 to your computer and use it in GitHub Desktop.
Save tenderlove/231428 to your computer and use it in GitHub Desktop.
class Fancy
def self.test
puts "Ask me a question:"
t = gets
puts "You said: #{t}"
puts "What about this other thing?"
t2 = gets
puts "Now you said: #{t2}"
end
end
require 'test/unit'
require 'stringio'
require 'rubygems'
require 'minitest/autorun'
class FancyTest < MiniTest::Unit::TestCase
def preload_stdin(*values)
orig_stdin = $stdin.dup
fake_stdin = StringIO.new(values.join("\n"))
$stdin = fake_stdin
yield
ensure
$stdin = orig_stdin
end
def test_can_provide_input
output,err = capture_io do
preload_stdin("Some text","Some other text") do
Fancy.test
end
end
assert_match /You said: Some text/, output
assert_match /Now you said: Some other text/, output
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment