Skip to content

Instantly share code, notes, and snippets.

@ravicious
Forked from tenderlove/input-test-two.rb
Created November 12, 2009 14:08
Show Gist options
  • Save ravicious/232917 to your computer and use it in GitHub Desktop.
Save ravicious/232917 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