0

我是如此接近。我正在为此方法创建一个简单的 rspec 测试。我希望它检查该方法是否接收到“板”并返回值 c3。

当前的测试看起来像这样......

  describe 'attempt_win' do
    it 'should contain all possible ai win moves' do
      @player_computer.attempt_win(@board).should include(@ai_winmoves)
    end
    it 'should return a win move' do
      # need to fake grid input here
      board = Board.new
      board.grid[:a1] = "O"
      board.grid[:b2] = "O"

      @player_computer.attempt_win(board).should  include("c3")  #how to say return 'c3' ??
    end
  end

我正在测试的方法看起来像这样......

  def attempt_win(board)

    @keys_with_o = board.grid.select{ |k, v| v == "O" }.keys  # find Os on the board

    @answers_array = [] # initialize answers array

    @ai_winmoves.each do |k, v| # go through each win move in the ai_winmoves array above.         
      ai_keys = v.select{ |k, v| v == "O"}.keys # grab all computer player's Os from the value hash

      intersection = ai_keys & @keys_with_o 
      # get common elements between two arrays..note: keys_with_o = all current O's on game board
      if intersection.length >=2 # when two intersections exist it means two O's are on the board

        @answers_array << k # add to answers array per iteration

        @answers_array.each do |key|
          # answer = @anskey[@thing.last].to_sym
          puts "which moves can ai win with?"
          puts @anskey[key]
          answer = @anskey[key].to_sym
          puts "attempt win"
          puts answer

          if board.grid[answer] == " " #if win move space is empty take it
            @move = answer               
          else #check for a block move  
            # attempt_block    # handled at line 162               
          end
        end
      end
    end # END @ai_winmoves.each do |k,v|
  end

当我运行 rspec spec 时,与此测试相关的输出如下所示...

    attempt_win
      should contain all possible ai win moves
  which moves can ai win with?
  c3
  attempt win
  c3
      should return a win move (FAILED - 1)

所以我需要以某种方式在我的 rspec 期望中捕获“c3”

有人指出我正确的方向吗?

4

1 回答 1

0

好的,所以测试结果是这样的......

  describe 'attempt_win' do
    it 'should contain all possible ai win moves' do
      @player_computer.attempt_win(@board).should include(@ai_winmoves)
    end
    it 'should return a win move' do
      # need to fake grid input here
      myboard = Board.new
      myboard.grid[:a1] = "O"
      myboard.grid[:b2] = "O"

      @player_computer.attempt_win(myboard).should  eq(:c3)
    end
  end

问题出在我如何在方法中返回我的答案...... Rspec 让我正确地写了它......

  def attempt_win(board)

    @keys_with_o = board.grid.select{ |k, v| v == "O" }.keys  # find Os on the board

    @answers_array = [] # initialize answers array

    @ai_winmoves.each do |k, v| # go through each win move in the ai_winmoves array above.         
      ai_keys = v.select{ |k, v| v == "O"}.keys # grab all computer player's Os from the value hash

      intersection = ai_keys & @keys_with_o 
      # get common elements between two arrays..note: keys_with_o = all current O's on game board
      if intersection.length >=2 # when two intersections exist it means two O's are on the board

        @answers_array << k # add to answers array per iteration

        @answers_array.each do |key|

          # puts "which moves can ai win with?"
          # puts @anskey[key]
          answer = @anskey[key].to_sym
          # puts "attempt win"
          # puts answer

          if board.grid[answer] == " " #if win move space is empty take it
            @move = answer              
          else #check for a block move  
            # attempt_block    # handled at line 162               
          end
        end
      end
    end # END @ai_winmoves.each do |k,v|
    return @move 
  end
于 2012-10-15T18:12:00.120 回答