Ruby-rspec测试失败(方法未定义),但通过了本地IDE

时间:2019-01-06 23:31:19

标签: ruby rspec

我用ruby(不是rails)编写了一些简单的方法,并使用常规的rspec测试了它们。它们全都通过我的系统,但是其中两个在自动分级机中失败,如果我在cloud9上运行它们,它们也会失败。这个怎么可能?我已经在Windows上搜索了rspec的任何特殊问题,但至少从3.2版开始就没有了。(我使用的是3.8)

这是我的IDE中的输出:

    $ rspec

Ruby intro part 1
  #sum
    should be defined
    returns correct sum [20 points]
    works on the empty array [10 points]
  #max_2_sum
    should be defined
    returns the correct sum [7 points]
    works even if 2 largest values are the same [3 points]
    returns zero if array is empty [10 points]
    returns value of the element if just one element [10 points]
  #sum_to_n
    should be defined
    returns true when any two elements sum to the second argument [30 points]
    returns false for any single element array [5 points]
    returns false for an empty array [5 points]

#hello
  should be defined
  The hello method returns the correct string [30 points]

#starts_with_consonant?
  should be defined
  classifies true cases [10 points]
  classifies false cases [10 points]
  works on the empty string [5 points]
  works on nonletters [5 points]

#binary_multiple_of_4?
  should be defined
  classifies valid binary numbers [30 points]
  rejects invalid binary numbers [10 points]

BookInStock
  should be defined
  getters and setters
    should set ISBN [10 points]
    should set price [10 points]
    should be able to change ISBN [10 points]
    should be able to change price [10 points]
  constructor
    should reject invalid ISBN number [10 points]
    should reject zero price [10 points]
    should reject negative price [10 points]
  #price_as_string
    should be defined
    should display 33.95 as "$33.95" [10 points]
    should display 1.1 as $1.10 [10 points]
    should display 20 as $20.00 [10 points]

Finished in 0.03195 seconds (files took 0.30126 seconds to load)
34 examples, 0 failures

现在是平地机...

Ruby intro part 1
  #sum
    should be defined
    returns correct sum [20 points]
    works on the empty array [10 points]
  #max_2_sum
    should be defined (FAILED - 1)
    returns the correct sum [7 points] (FAILED - 2)
    works even if 2 largest values are the same [3 points] (FAILED - 3)
    returns zero if array is empty [10 points] (FAILED - 4)
    returns value of the element if just one element [10 points] (FAILED - 5)
  #sum_to_n
    should be defined
    returns true when any two elements sum to the second argument [30 points]
    returns false for the single element array [5 points]
    returns false for the empty array [5 points]

Failures:

  1) Ruby intro part 1 #max_2_sum should be defined
     Failure/Error: expect { max_2_sum([1,2,3]) }.not_to raise_error

       expected no Exception, got # with backtrace:

  2) Ruby intro part 1 #max_2_sum returns the correct sum [7 points]
     Failure/Error: raise 'NonInteger' if arr.all?(Integer) == false

     ArgumentError:
       wrong number of arguments (1 for 0)

  3) Ruby intro part 1 #max_2_sum works even if 2 largest values are the same [3 points]
     Failure/Error: raise 'NonInteger' if arr.all?(Integer) == false

     ArgumentError:
       wrong number of arguments (1 for 0)

  4) Ruby intro part 1 #max_2_sum returns zero if array is empty [10 points]
     Failure/Error: raise 'NonInteger' if arr.all?(Integer) == false

     ArgumentError:
       wrong number of arguments (1 for 0)

  5) Ruby intro part 1 #max_2_sum returns value of the element if just one element [10 points]
     Failure/Error: raise 'NonInteger' if arr.all?(Integer) == false

     ArgumentError:
       wrong number of arguments (1 for 0)

Finished in 0.01805 seconds (files took 11744 minutes 10 seconds to load)
12 examples, 5 failures

Failed examples:

rspec your_code.rb:157 # Ruby intro part 1 #max_2_sum should be defined
rspec your_code.rb:160 # Ruby intro part 1 #max_2_sum returns the correct sum [7 points]
rspec your_code.rb:164 # Ruby intro part 1 #max_2_sum works even if 2 largest values are the same [3 points]
rspec your_code.rb:167 # Ruby intro part 1 #max_2_sum returns zero if array is empty [10 points]
rspec your_code.rb:170 # Ruby intro part 1 #max_2_sum returns value of the element if just one element [10 points]

BookInStock
  should be defined
  getters and setters
    should set ISBN [10 points]
    should set price [10 points]
    should be able to change ISBN [10 points]
    should be able to change price [10 points]
  constructor
    should reject invalid ISBN number [10 points]
    should reject zero price [10 points]
    should reject negative price [10 points]
  #price_as_string
    should be defined
    should display 33.95 as "$33.95" [10 points]
    should display 1.1 as $1.10 [10 points]
    should display 20 as $20.00 [10 points]

Finished in 0.00328 seconds (files took 11744 minutes 10 seconds to load)
12 examples, 0 failures

#hello
  should be defined
  The hello method returns the correct string [30 points]

#starts_with_consonant?
  should be defined
  classifies true cases [10 points]
  classifies false cases [10 points]
  works on the empty string [5 points]
  works on nonletters [5 points]

#binary_multiple_of_4?
  should be defined (FAILED - 1)
  classifies valid binary numbers [30 points] (FAILED - 2)
  rejects invalid binary numbers [10 points] (FAILED - 3)

Failures:

  1) #binary_multiple_of_4? should be defined
     Failure/Error: expect { binary_multiple_of_4?("yes") }.not_to raise_error()#::NoMethodError)

       expected no Exception, got # with backtrace:

  2) #binary_multiple_of_4? classifies valid binary numbers [30 points]
     Failure/Error: return false unless s.chars.all?(/[01]/) #

     ArgumentError:
       wrong number of arguments (1 for 0)

  3) #binary_multiple_of_4? rejects invalid binary numbers [10 points]
     Failure/Error: return false unless s.chars.all?(/[01]/) #

     ArgumentError:
       wrong number of arguments (1 for 0)

Finished in 0.00239 seconds (files took 11744 minutes 10 seconds to load)
10 examples, 3 failures

Failed examples:

rspec your_code.rb:175 # #binary_multiple_of_4? should be defined
rspec your_code.rb:178 # #binary_multiple_of_4? classifies valid binary numbers [30 points]
rspec your_code.rb:186 # #binary_multiple_of_4? rejects invalid binary numbers [10 points]

编辑:

以下是通过/失败的方法:

def max_2_sum(arr)

  # takes an array of integers as an argument and returns the sum of its two largest elements. 
  # For an empty array it should return zero. For an array with just one element, it should return that element.

  raise 'NonInteger' if arr.all?(Integer) == false
  return 0 if arr.empty?
  return arr[0] if arr.length == 1
  return sum(arr) if arr.length == 2
  arr.sort!
  sum(arr[-2..-1])

end

def binary_multiple_of_4?(s) 

  if s.length == 1
    return true if s == '0'
    return false if s == '1'
  else
    return false unless s.chars.all?(/[01]/)
    s[-1] == '0' && s[-2] == '0'
  end

end

以下是测试:

  describe '#max_2_sum' do
    it 'should be defined' do
      expect { max_2_sum([1, 2, 3]) }.not_to raise_error
    end
    it 'returns the correct sum [7 points]', points: 7 do
      expect(max_2_sum([1, 2, 3, 4, 5])).to be_a_kind_of Integer
      expect(max_2_sum([1, -2, -3, -4, -5])).to eq(-1)
    end
    it 'works even if 2 largest values are the same [3 points]', points: 3 do
      expect(max_2_sum([1, 2, 3, 3])).to eq(6)
    end
    it 'returns zero if array is empty [10 points]', points: 10 do
      expect(max_2_sum([])).to be_zero
    end
    it 'returns value of the element if just one element [10 points]', points: 10 do
      expect(max_2_sum([3])).to eq(3)
    end
  end

describe '#binary_multiple_of_4?' do
  it 'should be defined' do
    expect { binary_multiple_of_4?('yes') }.not_to raise_error#::NoMethodError)
  end
  it 'classifies valid binary numbers [30 points]' , points: 30 do
    %w[1010101010100 0101010101010100 100 0].each do |string|
      expect(binary_multiple_of_4?(string)).to be_truthy,  "Incorrect results for input: \"#{string}\""
    end
    %w[101 1000000000001].each do |string|
      expect(binary_multiple_of_4?(string)).not_to be_truthy,  "Incorrect results for input: \"#{string}\""
    end
  end
  it 'rejects invalid binary numbers [10 points]' , points: 10 do
    expect(binary_multiple_of_4?('a100')).to be_falsy, "'a100' is not a valid binary number!"
    expect(binary_multiple_of_4?('')).to be_falsy, 'The empty string is not a valid binary number!'
  end
end

为什么在某些环境中s.chars.all?(/[01]/)arr.all?(Integer)会导致ArgumentError而不在其他环境中导致ArgumentError?该文档没有给出任何答案。更重要的是,我该如何解决?

1 个答案:

答案 0 :(得分:0)

参数签名

arr.all?(Integer)
arr.all?(/[01]/)

仅在Ruby 2.5中受支持。您可以看到讨论该博客文章

https://blog.bigbinary.com/2018/01/02/ruby-2-5-enumerable-predicates-accept-pattern-argument.html

在旧版Ruby中,您必须将一个块传递给all?,例如:

arr.all? { |el| el.is_a?(Integer) }
arr.all? { |el| el =~ /[01]/ }

从您的评论看来,您的本地Ruby环境和Cloud 9正在运行不同的版本。