我有这个方法作为一个更大的类的一部分。我正在尝试为它编写一个测试,但我是 rspec 的新手,我有点难过......如果我注释掉 9.times 循环中的所有内容,我可以测试“drawgrid”。但如果我取消注释该代码,当前测试将失败。我需要测试 play 方法...运行游戏。它放置'drawgrid'......运行游戏序列9次,每回合后放置'drawgrid'。但我不知道该怎么做。非常感谢任何指针。
以下是播放方法及其当前规格
def play
#draw the board
puts drawgrid
#make a move
turn = 0
9.times do
if turn.even?
@player = @player_h.move_human("X", @board)
@move = @player.to_sym
@marker = @player_h.boardpiece
does_move_exist(@move,@marker)
is_a_human_win(@board)
else
@player = @player_c.move_computer("O", @board)
@move = @player
@marker = @player_c.boardpiece
does_move_exist(@move,@marker)
is_a_computer_win(@board)
end
puts drawgrid
turn += 1
end # 9.times ends
end
当前规格....
describe 'play method' do
it 'draws the game grid' do
@player_human = Player.new('X')
@player_computer = Player.new('O')
@board = Board.new
@game = Game.new(@player_human, @player_computer, @board)
@game.should_receive(:puts).with("\na | | \n----------\nb | | \n----------\nc | | \n----------\n 1 2 3\n")
@game.play
end
end
describe '9.times' do
it 'runs game sequence 9 times...once per board spot' do
@player_human2 = Player.new('X')
@player_computer2 = Player.new('O')
@board2 = Board.new
@game2 = Game.new(@player_human2, @player_computer2, @board2)
turn = 0
9.times do
if turn.even?
@player_human2.should_receive(:puts).with("human move...")
@player_human2.stub(:gets).and_return("b2")
else
@player_human2.should_receive(:puts).with("computer move...")
@player_human2.stub(:gets).and_return("a1")
end
turn += 1
end
@game2.play
end
end