我将此方法作为更大类的一部分。我试图为它写一个测试,但是我对rspec很陌生,而且我有点难倒...如果我在9.times循环中注释掉所有内容,我可以测试'drawgrid'。但如果我取消注释该代码,则当前测试失败。我需要测试游戏方法......来运行游戏。它把'drawgrid'...运行游戏序列9次,每次回合后放置'drawgrid'。但我不知道如何做到这一点。任何指针都非常感谢。如何为一种方法编写rspec测试
下面是游戏方法,它是当前规范
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
另外根据Dave Giunta在他的回答中所说的话,我强烈建议阅读[RSpec Book]的第一部分(http://pragprog.com/book/achbd/the-rspec-book),标题为“RSpec&Cucumber入门'。在这9章中,本书让读者通过构建数字猜测游戏的练习,并说明如何使用规范和功能编写易于维护的代码。 –
感谢您的回复。我现在已经和我一起阅读了这篇文章......你知道那个地方,在你阅读完路径之后,你会尝试走这条路吗?那就是我现在的地方。试图让我的大脑和我的手指“理解”。我有这个文本以及在github上列出的信息... https://github.com/rspec/rspec-expectations和... https://github.com/rspec/rspec-mocks...使用as参考资料... sooo多读书总是:) – thefonso