当assert_equal两个2D数组时,Minitest ArgumentError

时间:2017-01-21 17:17:38

标签: ruby minitest

我有以下test case

def test_primary_key
  big = [['foo','bar@','baz'],
         ['cat','moew@','purr'],
         ['dog','bark@','woof'],
         ['mew', 'two@', 'blue']]

  little = [['foo','bar@','baz'],
            ['dog','moew@','woof'],
            ['dog','bark@','woof']]

  args = { big: big, little: little }
  differ = Differ.new(args)
  # assert_equal [["mew", "two@", "blue"]], differ.diff # ArgumentError: wrong number of arguments (given 2, expected 0)
  assert (differ.diff - [["mew", "two@", "blue"]]).empty?
end

如我在尝试assert_equal这两个数组的评论中所示,我得到的是ArgumentError

发生了什么事?我不明白为什么我不能断言两个数组相等?

1 个答案:

答案 0 :(得分:1)

我克隆了您的回购并发现了问题。

首先,这不是assert_equal的问题。

问题是您设置的attr_reader名为:diff,而Minitest也设置名为diff的方法。这导致了冲突。

尝试将代码更改为以下内容:

require 'test_helper'
require 'csv'

class DifferTest < Minitest::Test
  include LiffyDiffy
  attr_reader :big, :little, :args, :diff_test, :headers, :differ
  def setup
    @big = [['foo','bar','baz'],
                  ['cat','moew','purr'],
                  ['dog','bark','woof'],
                  ['snake','slurp','slither']]
    @little = [['foo','bar','baz'],
                     ['dog','bark','woof'],
                     ['snake','slurp','slither']]
    @args = {big: big, little: little}
    @diff_test = [['cat','moew','purr']]
    @headers = ['foo','bar','baz']
    @differ = Differ.new(args)

    assert_equal [["mew", "two@", "blue"]], differ.diff
    assert (differ.diff - [["mew", "two@", "blue"]]).empty?

  end

  def test_interface
    interface = {diff: diff_test, headers: headers}
    assert_equal interface, differ.interface
  end

  def test_differ
    assert Differ.new(big: big, little: little)
  end

  def test_diff
    assert_equal diff_test, differ.diff
  end

  def test_headers
    assert_equal headers, differ.headers
  end

end

现在正在抛出正确的错误(测试失败):

 FAIL["test_differ", DifferTest, 0.002584386000307859]
 test_differ#DifferTest (0.00s)
        --- expected
        +++ actual
        @@ -1,7 +1,7 @@
         [
           [
        -    "mew",
        -    "two@",
        -    "blue"
        +    "cat",
        +    "moew",
        +    "purr"
           ]
         ]
        /data/ruby/liffy_diffy/test/liffy_diffy/differ_test.rb:20:in `setup'