迷失在rspec的海洋中

时间:2012-10-13 20:53:26

标签: ruby rspec

我很亲密。我正在为这个方法创建一个简单的rspec测试。我希望它检查该方法是否接收到了' board'并返回值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规范时,与此测试相关的输出看起来像这样......

    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)

所以我需要以某种方式捕获&#34; c3&#34;在我的rspec期望

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

1 个答案:

答案 0 :(得分: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