瞭解如何測試Highline的最佳方法是查看作者如何測試他的包。
class TestHighLine < Test::Unit::TestCase
def setup
@input = StringIO.new
@output = StringIO.new
@terminal = HighLine.new(@input, @output)..
end
..
def test_agree
@input << "y\nyes\nYES\nHell no!\nNo\n"
@input.rewind
assert_equal(true, @terminal.agree("Yes or no? "))
assert_equal(true, @terminal.agree("Yes or no? "))
assert_equal(true, @terminal.agree("Yes or no? "))
assert_equal(false, @terminal.agree("Yes or no? "))
....
@input.truncate(@input.rewind)
@input << "yellow"
@input.rewind
assert_equal(true, @terminal.agree("Yes or no? ", :getc))
end
def test_ask
name = "James Edward Gray II"
@input << name << "\n"
@input.rewind
assert_equal(name, @terminal.ask("What is your name? "))
....
assert_raise(EOFError) { @terminal.ask("Any input left? ") }
end
等等,如他的代碼所示。您可以在highline source中找到此信息,密切關注我在鏈接中突出顯示的設置。
注意他是如何使用STDIN IO管道來操作鍵盤上鍵盤的。
這是什麼表示,真的,你不需要使用highline
來測試這種事情。他的測試中的設置在這裏非常重要。隨着他使用StringIO作爲對象。