我正在使用RSpec来测试我的摇滚纸剪刀游戏。包括在我begin_game
功能我有以下代码:Rspec用户输入while循环测试
user_input = gets.chomp.downcase.to_sym
while !choices.include? user_input
puts "Please choose a valid selection : rock, paper, or scissors"
user_input = gets.chomp.downcase.to_sym
end
我想测试不同的可能user_inputs。我试过这个:
let(:new_game) {RockPaperScissors.new}
.......
context 'validate that the user input is one of the given choices' do
it 'should prompt the user for a new input if the original one is invalid' do
new_game.stub(:gets) {"r"}
expect(new_game.begin_game).to eq("Please choose a valid selection : rock, paper, or scissors")
end
end
但是这会导致无限循环的“Please choose valid selection ...”被输出到终端。我阅读了RSpec模拟文档,但我很难理解。
,我认为你应该做'STDIN.stub(:获得)'或'Kernel.stub(:获得)',而不是'new_game.stub' – bjhaid