了解如何测试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作为对象。