首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >迷失在rspec的海洋中

迷失在rspec的海洋中
EN

Stack Overflow用户
提问于 2012-10-14 04:53:26
回答 1查看 95关注 0票数 0

我就快成功了。我正在为这个方法创建一个简单的rspec测试。我想让它检查该方法是否接收到了'board‘,并返回了一个值c3。

现在的测试是这样的.

代码语言:javascript
运行
复制
  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

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

代码语言:javascript
运行
复制
  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时,与此测试相关的输出如下所示……

代码语言:javascript
运行
复制
    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

有人给我指了个方向吗?

EN

回答 1

Stack Overflow用户

发布于 2012-10-16 02:12:00

测试结果是这样的.

代码语言:javascript
运行
复制
  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

问题在于我在method...Rspec中返回答案的方式让我正确地写出了答案……

代码语言:javascript
运行
复制
  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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12876905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档