我在为使用 minitest 在 Ruby 中调用自身(游戏循环)的方法开发单元测试时遇到问题。我一直在尝试用我的输入来存根我试图在所述游戏循环中调用的方法。这是游戏循环:
#main game loop
def playRound
#draw board
@board.printBoard
#get input
playerInput = gets.chomp #returns user input without ending newline
#interpret input, quitting or beginning set selection for a player
case playerInput
when "q"
quit
when "a", "l"
set = getPlayerSet()
if(playerInput == "a")
player = 1
else
player = 2
end
when "h"
if @hintsEnabled
giveHint
playRound
else
puts "Hints are disabled"
playRound
end
else
puts "Input not recognized."
end
if(set != nil)
#have board test set
checkSet(set, player)
end
#check if player has quitted or there are no more valid sets
unless @quitted || @board.boardComplete
playRound
end
end
其中大部分最终是无关紧要的,我试图测试的是这个 switch 语句正在调用正确的方法。目前我正试图通过存根被调用的方法来引发错误(我的测试assers_raise的)来规避循环:
def test_playRound_a_input_triggers_getPlayerSet
@game.stub :getPlayerSet, raise(StandardError) do
assert_raises(StandardError) do
simulate_stdin("") {
@game.playRound
}
end
end
end
但是,这种方法似乎不起作用,因为 Minitest 将上述测试的结果记录为消息中的错误
E
错误:
TestGame#test_playRound_a_input_triggers_getPlayerSet:
StandardError:StandardError
test_game.rb:136:in `test_playRound_a_input_triggers_getPlayerSet'
如果有人对我有任何建议或指导,我将不胜感激,因为我不知道出了什么问题