【发布时间】:2016-11-17 11:49:55
【问题描述】:
我在class QuestionList中有如下方法
def ask_all
@questions.each do |question|
question.ask
@player.add_answer(gets.chomp)
end
end
这些问题有整数答案,并且答案对于这个测试不必是正确的 - 只需要接收一个整数并将其添加到列表@player.answers 中,使用class Player 中的以下方法
def add_answer(answer)
@answers << answer
end
当对方法进行单元测试时,如何模拟用户对QuestionList.ask_all 的gets.chomp 的输入:
class QuestionListTest < Test::Unit::TestCase
def setup
...
end
def test_ask_all
#unit test for 'QuestionList.ask_all' here
end
end
【问题讨论】:
-
@player.add_answer((1..10).to_a.sample) -
我如何在
< Test::Unit::TestCase类中实现它? -
显然用
return (1..10).to_a.sample模拟@player#add_answer。 -
我仍然不知道如何在我的
test_ask_all方法中实现它?@player.add_answer将答案添加到列表并返回列表。所以,在我的测试方法中,我需要类似于assert_equal([1], @questions.ask_all)的东西,但是ask_all里面有@player.add_answer(gets.chomp)行,所以gets.chomp不可能在测试类中执行,我不知道如何执行您的建议 - 只有在测试类中运行时,ask_all才会使用行@player.add_answer((1..10).to_a.sample)执行,但在主程序中运行时不会执行。 -
@mudasobwa 我在我的 OP 中添加了更多信息
标签: ruby unit-testing user-input stdin