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 に答える