I have this method as part of a larger class. I'm trying to write a test for it but I'm new to rspec and I'm kinda stumped...I can test 'drawgrid' if I comment out everything in the 9.times loop. but if I uncomment that code the current test fails. I need to test that the play method...runs the game. that it puts 'drawgrid'...runs the game sequence 9 times putting the 'drawgrid' after each turn. But I'm not sure how to do this. Any pointers are greatly appreciated.
Below is the play method and it's current spec
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
current spec....
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
Getting Started with RSpec & Cucumber. In those 9 chapters, the book takes the reader through an exercise of building a number guessing game, and illustrate how to use specs & features to write easily maintainable code.