我花了3个小时主演这个并需要一些帮助。
我是RSpec的新手,我正在尝试为我所谓的“游戏”类的行为编写测试
我想测试一下,当调用game.play向输出发送一个3x3网格时....这就是我想要做的。我有RSpec书,我正在努力想出这个,但我很难过。 我已将我认为关键的地方标记为“FIXME”
这是我到目前为止的测试...
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
这是我正在构建这个测试的类(是的,我知道它是倒退的...我应该编写测试,然后编写代码......但就像我说的那样,我是一个菜鸟。 ..整个游戏代码已经写好了...我现在真的想要了解RSpec。)...
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
非常感谢任何指导。
答案 0 :(得分:0)
IIRC在RSpec书中他们传递了一个IO对象,在测试中被嘲笑,但是如果你希望STDOUT
接收到这些,它也应该有效:
STDOUT.should_receive(:puts).with("foo")