0

最后两个测试都单独工作......但是当两者都设置为运行(非挂起)时,我遇到了问题。

问题:我可以创建一个将两者合二为一的测试吗?这看起来如何?(是的,我是 rspec 的新手)

require_relative '../spec_helper'

# the universe is vast and infinite....and...it is empty
describe "tic tac toe game" do
  context "the game class" do

    before (:each) do
      player_h = Player.new("X")
      player_c = Player.new("O")
      @game = Game.new(player_h, player_c)
    end

   it "method drawgrid must return a 3x3 game grid" do
      @game.drawgrid.should eq("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n  1 2 3 \n")
      @game.drawgrid
   end
   #FIXME - last two test here - how to merge into one?
   it "play method must display 3x3 game grid" do
      STDOUT.should_receive(:puts).and_return("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n  1 2 3 \n").with("computer move")
      @game.play
    end
    it "play method must display 3x3 game grid" do
      STDOUT.should_receive(:puts).with("computer move")
      @game.play
    end
  end
end

仅供参考这里是包含播放方法的代码

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 which is used by external player class
    $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
    #external call to player class
    @player = @player_c.move_computer("O")
  end
end

player_h = Player.new("X")
player_c = Player.new("O")


game = Game.new(player_h, player_c)
game.play

更新 - 测试错误输出只是为了完整起见......这是运行 rspec 规范的完整输出......

    gideon@thefonso ~/Documents/ca_ruby/rubytactoe (now-with-rspec)$ rspec spec

    a  | |  
    ----------
    b  | |  
    ----------
    c  | |  
    ----------
      1 2 3 
    computer move

    tic tac toe game
      the game class
        method drawgrid must return a 3x3 game grid

    An error occurred in an after(:each) hook
      RSpec::Mocks::MockExpectationError: (#<IO:0x007f948406fcf0>).puts(any args)
        expected: 1 time
        received: 0 times
      occurred at /Users/gideon/Documents/ca_ruby/rubytactoe/spec/game_spec.rb:18:in `block (3 levels) in <top (required)>'

        play method must display 3x3 game grid (FAILED - 1)

    An error occurred in an after(:each) hook
      RSpec::Mocks::MockExpectationError: (#<IO:0x007f948406fcf0>).puts("computer move")
        expected: 1 time
        received: 0 times
      occurred at /Users/gideon/Documents/ca_ruby/rubytactoe/spec/game_spec.rb:22:in `block (3 levels) in <top (required)>'

        play method must display 3x3 game grid (FAILED - 2)

    tic tac toe game
      the player class
        must have a human player X
        must have a computer player O

    Failures:

      1) tic tac toe game the game class play method must display 3x3 game grid
         Failure/Error: STDOUT.should_receive(:puts).and_return("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n  1 2 3 \n").with("computer move")
         NoMethodError:
           undefined method `with' for #<Proc:0x007f9484341168>
         # ./spec/game_spec.rb:18:in `block (3 levels) in <top (required)>'

      2) tic tac toe game the game class play method must display 3x3 game grid
         Failure/Error: @game.play
           #<IO:0x007f948406fcf0> received :puts with unexpected arguments
             expected: ("computer move")
                  got: ("\na  | |  \n----------\nb  | |  \n----------\nc  | |  \n----------\n  1 2 3 \n")
         # ./lib/game.rb:37:in `puts'
         # ./lib/game.rb:37:in `play'
         # ./spec/game_spec.rb:23:in `block (3 levels) in <top (required)>'

    Finished in 0.00457 seconds
    5 examples, 2 failures

    Failed examples:

    rspec ./spec/game_spec.rb:17 # tic tac toe game the game class play method must display 3x3 game grid
    rspec ./spec/game_spec.rb:21 # tic tac toe game the game class play method must display 3x3 game grid
    gideon@thefonso ~/Documents/ca_ruby/rubytactoe (now-with-rspec)$ 
4

1 回答 1

1

问题应该包括你遇到了什么的“问题”;他们帮助诊断出了什么问题。

这是一个答案,之后有一个警告;还有其他方法。

it "play method must display 3x3 game grid" do
  STDOUT.should_receive(:puts).with("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n  1 2 3 \n").twice.ordered
  STDOUT.should_receive(:puts).with("computer move").once.ordered

  @game.play
  @game.play
end

需要注意的是:您现在的测试方式将无法维持。我可能不会移动测试期间继续检查输出,而是单独测试输出,但隔离移动机制,以便您可以检查,例如,数组值或指示它是谁移动的标志,等等.

您还需要决定是否真的要测试顺序移动,而不是移动计算逻辑。连续移动需要像上面那样刺激性的测试结构,或者执行测试顺序,或者以其他方式确保移动的顺序是正确的。

重新考虑您正在测试的具体内容和时间可能更有意义。

于 2012-09-04T04:16:44.067 回答