gpt4 book ai didi

ruby - 如何使用 Highline 测试 rspec 用户输入和输出?

转载 作者:数据小太阳 更新时间:2023-10-29 08:15:58 26 4
gpt4 key购买 nike

我想测试对用户输入的响应。使用 Highline 查询该输入:

def get_name
return HighLine.new.ask("What is your name?")
end

我想做类似于 this question 的事情并将其放入我的测试中:

STDOUT.should_receive(:puts).with("What is your name?")
STDIN.should_receive(:read).and_return("Inigo Montoya")
MyClass.new.get_name.should == "Inigo Montoya"

使用 Highline 执行此操作的正确方法是什么?

最佳答案

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

关于ruby - 如何使用 Highline 测试 rspec 用户输入和输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14250005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com