没有救援的其他没用

时间:2017-03-03 23:38:28

标签: ruby unit-testing error-handling

我的代码出现问题,似乎无法弄清楚我需要改变什么。这是我的三个文件,底部是我得到的错误。

require './PokerHand'
    require "./Constants"
    require 'minitest/autorun'

    class TestClass < MiniTest::Test
      include Constants

        def test_1
          arr1 = [Card.new(2, "S"), Card.new(3, "S"),
             Card.new(4, "S"), Card.new(5, "S"),
             Card.new(6, "S")]
          ph1 = PokerHand.new(arr1)
          ph1.classify
          assert_equal STRAIGHT_FLUSH, ph1.hand_type
        end 

       def test_2
        arr2 = [Card.new(9, "C"), Card.new(9, "S"),
             Card.new(9, "H"), Card.new(9, "D"),
             Card.new(11, "S")]
        ph2 = PokerHand.new(arr2)
        ph2.classify
        assert_equal FOUR_OF_A_KIND, ph2.hand_type
      end

      def test_3
        arr3 = [Card.new(4, "C"), Card.new(9, "S"),
             Card.new(9, "H"), Card.new(9, "D"),
             Card.new(9, "C")]
        ph3 = PokerHand.new(arr3)
        ph3.classify
        assert_equal FOUR_OF_A_KIND, ph3.hand_type
      end

新文件Pokerhand.rb:

require "./Constants"
require "./Card"
require "./Deck"
require "./CardSpaceship"

class PokerHand < Deck
  include Constants
  attr_reader :hand_type

  def initialize(the_cards)
    @cards = [ ]
    @hand_type = UNCLASSIFIED
  for card in the_cards
     @cards << card
  end
  end


# Determine hand type of PokerHand object.
def classify

  @cards.sort!

  # Straight flush 
  if @cards[0].rank == @cards[1].rank +1 &&
     @cards[1].rank == @cards[2].rank +1 &&
     @cards[2].rank == @cards[3].rank +1 &&
     @cards[3].rank == @cards[4].rank +1 &&  
     @cards[0].suit == @cards[1].suit &&
     @cards[1].suit == @cards[2].suit &&
     @cards[2].suit == @cards[3].suit &&
     @cards[3].suit == @cards[4].suit
     @hand_type = STRAIGHT_FLUSH
  end
end

新文件test2.rb:

require './PokerHand'
require "./Constants"
require 'minitest/autorun'

class TestClass < MiniTest::Test
  include Constants

    def test_1
      arr1 = [Card.new(2, "S"), Card.new(3, "S"),
         Card.new(4, "S"), Card.new(5, "S"),
         Card.new(6, "S")]
      ph1 = PokerHand.new(arr1)
      ph1.classify
      assert_equal STRAIGHT_FLUSH, ph1.hand_type
    end 

收到错误:

TestClass#test_1:
PokerHand.rb:145: warning: else without rescue is useless
C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': JulianHansen_P5/PokerHand.rb:30: syntax error, unexpected tINTEGER, expecting keyword_then or ';' or '\n' (SyntaxError)
  if @cards[0].rank == @cards[1].rank +1 &&
                                        ^
PokerHand.rb:31: syntax error, unexpected tINTEGER, expecting keyword_end
    @cards[1].rank == @cards[2].rank +1 &&

1 个答案:

答案 0 :(得分:2)

当您不正确地设置warning: else without rescue is useless / if语句时,会显示else警告。

if true
  puts 'hi''
end # end shouldn't be here
else
  puts 'whoops'
end

您应该在代码中找到并更正它,尽管这不是造成致命错误的原因。

+1语句中的if s需要空格或括号:

if @cards[0].rank == @cards[1].rank + 1 &&

if @cards[0].rank == (@cards[1].rank +1) &&