I’ve spent 3 hrs starring at this and need some help.
I’m pretty new to RSpec and I’m trying to write test for my behavior of a class called “game”
I want to test that when called game.play sends a 3×3 grid to the output….that’s all I want to do. I have the RSpec book and I’m trying real hard to figure this out but I’m stumped.
I’ve marked the places I think are key as “FIXME”
here are my test so far…
require_relative '../spec_helper'
# the universe is vast and infinite....and...it is empty
describe "the game class" do
it "must output a 3x3 game grid on the CLI" do
player_h = double('human', :player_h => "X") # FIXME - do I stub or mock this?
player_c = double('computer', :player_c => "O")# FIXME - do I stub or mock this?
game = Game.new(player_h, player_c)
#FIXME - how do I get the line below to read this as if it where coming from SDOUT on the cli?
should_receive(:puts).with("a #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n")
game.play
end
it "must have a human player" do
pending "human is X"
end
it "must have a computer player" do
pending "ai is O"
end
end
and here is the class I’m building this test for (yes I know that’s backwards…I should be writing the test and THEN the code…but like I said, I’m a noob at this…the entire game code is already written…I’m really just tying to understand RSpec now.)…
require_relative "player"
#
#Just a Tic Tac Toe game class
class Game
#create players
def initialize(player_h, player_c)
#bring into existence the board and the players
@player_h = player_h
@player_c = player_c
#value hash for the grid lives here
$thegrid = {
:a1=>" ", :a2=>" ", :a3=>" ",
:b1=>" ", :b2=>" ", :b3=>" ",
:c1=>" ", :c2=>" ", :c3=>" "
}
#make a global var for drawgrid used by player
$gamegrid = drawgrid
end
#display grid on console
def drawgrid
board = "\n"
board << "a #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n"
board << "----------\n"
board << "b #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n"
board << "----------\n"
board << "c #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n"
board << "----------\n"
board << " 1 2 3 \n"
return board
end
#start the game
def play
#draw the board
puts drawgrid
#make a move
#alternate player turns
end
end
Any guidance is much appreciated.
IIRC in the RSpec book they pass in an IO object, which is mocked in the tests, but it should also work if you expect
STDOUTto receive the puts: