0

我花了 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

非常感谢任何指导。

4

1 回答 1

0

IIRC 在 RSpec 书中他们传递了一个 IO 对象,该对象在测试中被模拟,但如果您希望STDOUT收到 put,它也应该工作:

STDOUT.should_receive(:puts).with("foo")
于 2012-08-31T00:01:05.100 回答