如何断言数组的内容,对顺序无动于衷

问题描述 投票:20回答:6

我有一个迷你测试规范:

it "fetches a list of all databases" do
  get "/v1/databases"
  json = JSON.parse(response.body)
  json.length.must_equal           Database.count
  json.map{|d| d["id"]}.must_equal Database.all.pluck(:id)
end

但是,这失败了:

Expected: [610897332, 251689721]
  Actual: [251689721, 610897332]

我可以同时订购它们,但这会增加混乱:

json.map{|d| d["id"]}.sort.must_equal Database.all.pluck(:id).sort

事实上,map{}已经与测试有点无关并且增加了混乱,我宁愿不添加更多。

是否有断言或帮助来测试enumerator1中的所有项目是否都在enumerator2

ruby-on-rails minitest assertion
6个回答
15
投票

TL; DR检查这一点的最直接方法是在检查数组之前对数组进行排序。

json.map{|d| d["id"]}.sort.must_equal Database.all.pluck(:id).sort

还在?好的。我们来谈谈比较数组中的元素。

事实上,地图{}已经与测试有点无关并且增加了混乱,我宁愿不再添加更多。

嗯,这是问题的一部分。您的JSON包含一个JSON对象数组,而调用Database.pluck将返回其他内容,可能是整数。您需要将JSON对象和查询转换为相同的数据类型。因此,说.map{}无关紧要是不准确的,如果它感觉像杂乱那么那是因为你在断言中做了很多事情。尝试拆分该行代码并使用意图揭示名称:

sorted_json_ids = json.map{|d| d["id"]}.sort
sorted_db_ids   = Database.order(:id).pluck(:id)
sorted_json_ids.must_equal sorted_db_ids

在测试中有更多的代码行,但更好地传达了意图。然而,我听到你的话语“无关紧要”和“混乱”在我脑海中回荡。我打赌你不喜欢这个解决方案。 “它的工作太多了!”并且“为什么我必须对此负责?”好吧好吧。我们有更多选择。更智能的断言怎么样?

RSpec有一个很好的小匹配器,名为match_array,几乎可以满足您的需求。它sorts and compares arrays并打印一个很好的消息,如果他们不匹配。我们可以做类似的事情。

def assert_matched_arrays expected, actual
  assert_equal expected.to_ary.sort, actual.to_ary.sort
end

it "fetches a list of all databases" do
  get "/v1/databases"
  json = JSON.parse(response.body)
  assert_matched_arrays Database.pluck(:id), json.map{|d| d["id"]}
end

“但这是一个断言而不是期望!”是的,我知道。放松。您可以通过调用infect_an_assertion将断言转换为期望值。但要做到这一点,您可能希望添加断言方法,以便可以在每个Minitest测试中使用它。所以在我的test_helper.rb文件中,我添加以下内容:

module MiniTest::Assertions
  ##
  # Fails unless <tt>exp</tt> and <tt>act</tt> are both arrays and
  # contain the same elements.
  #
  #     assert_matched_arrays [3,2,1], [1,2,3]

  def assert_matched_arrays exp, act
    exp_ary = exp.to_ary
    assert_kind_of Array, exp_ary
    act_ary = act.to_ary
    assert_kind_of Array, act_ary
    assert_equal exp_ary.sort, act_ary.sort
  end
end

module MiniTest::Expectations
  ##
  # See MiniTest::Assertions#assert_matched_arrays
  #
  #     [1,2,3].must_match_array [3,2,1]
  #
  # :method: must_match_array

  infect_an_assertion :assert_matched_arrays, :must_match_array
end

现在您的断言可用于任何测试,您的期望将在每个对象上可用。

it "fetches a list of all databases" do
  get "/v1/databases"
  json = JSON.parse(response.body)
  json.map{|d| d["id"]}.must_match_array Database.pluck(:id)
end

5
投票

MiniTest Rails Shoulda有一个assert_same_elements assertion,其中:

断言两个数组包含相同的元素,次数相同。基本上==,但无序。

assert_same_elements([:a, :b, :c], [:c, :a, :b]) => passes

3
投票

RSpec有一个match_array匹配器,无论顺序如何都能匹配2个数组。您可以执行以下操作在Minitest中创建类似的自定义匹配器:

module MiniTest::Assertions

  class MatchEnumerator
    def initialize(expected, actual)
      @expected = expected
      @actual = actual
    end

    def match()
      return result, message
    end

    def result()
      return false unless @actual.respond_to? :to_a
      @extra_items = difference_between_enumerators(@actual, @expected)
      @missing_items = difference_between_enumerators(@expected, @actual)
      @extra_items.empty? & @missing_items.empty?      
    end

    def message()
      if @actual.respond_to? :to_a
        message = "expected collection contained: #{safe_sort(@expected).inspect}\n"
        message += "actual collection contained: #{safe_sort(@actual).inspect}\n"
        message += "the missing elements were: #{safe_sort(@missing_items).inspect}\n" unless @missing_items.empty?
        message += "the extra elements were: #{safe_sort(@extra_items).inspect}\n" unless @extra_items.empty?
      else
        message = "expected an array, actual collection was #{@actual.inspect}"
      end

      message
    end

    private

    def safe_sort(array)
      array.sort rescue array
    end

    def difference_between_enumerators(array_1, array_2)
      difference = array_1.to_a.dup
      array_2.to_a.each do |element|
        if index = difference.index(element)
          difference.delete_at(index)
        end
      end
      difference
    end
  end # MatchEnumerator

  def assert_match_enumerator(expected, actual)
    result, message = MatchEnumerator.new(expected, actual).match
    assert result, message
  end

end # MiniTest::Assertions

Enumerator.infect_an_assertion :assert_match_enumerator, :assert_match_enumerator

您可以在以下测试中看到此自定义匹配器的运行情况:

describe "must_match_enumerator" do
  it{ [1, 2, 3].map.must_match_enumerator [1, 2, 3].map }
  it{ [1, 2, 3].map.must_match_enumerator [1, 3, 2].map }
  it{ [1, 2, 3].map.must_match_enumerator [2, 1, 3].map }
  it{ [1, 2, 3].map.must_match_enumerator [2, 3, 1].map }
  it{ [1, 2, 3].map.must_match_enumerator [3, 1, 2].map }
  it{ [1, 2, 3].map.must_match_enumerator [3, 2, 1].map }

  # deliberate failures
  it{ [1, 2, 3].map.must_match_enumerator [1, 2, 1].map }
end

因此,使用此自定义匹配器,您可以将测试重新编写为:

it "fetches a list of all databases" do
  get "/v1/databases"
  json = JSON.parse(response.body)
  json.length.must_equal           Database.count
  json.map{|d| d["id"]}.must_match_enumerator Database.all.pluck(:id)
end

2
投票

您可以在Ruby中使用array substraction,如下所示:

assert_empty(["A", "B"] - ["B", "A"])

但请注意以下事项:[“A”,“B”] - [“B”,“A”] == []但[“A”,“B”,“B”] - [“B” ,“A”] == []

因此,只有在那里有唯一值时才使用此技术。


1
投票

如果重复不是问题,一个选项是使用集合(标准ruby)

 require 'set'
 assert_equals [1,2,3].to_set, [3,2,1].to_set

其他明智的写你自己的断言方法(来自shoulda

module Minitest::Assertions
  def assert_same_elements(expected, current, msg = nil)
    assert expected_h = expected.each_with_object({}) { |e, h| h[e] ||= expected.select { |i| i == e }.size }
    assert current_h = current.each_with_object({}) { |e, h| h[e] ||= current.select { |i| i == e }.size}

    assert_equal(expected_h, current_h, msg)
  end
end

assert_same_elements [1,2,3,3], [3,2,1,3] # ok!
assert_same_elements [1,2,3,3], [3,2,1] # fails!

或者直接添加shoulda gem以获得更多。


0
投票

在性能不重要的测试场景中,您可以使用迭代和assert_include,例如:

test_result_items.each { |item| assert_include(expected_items, item) }

其中test_result_items是一个包含测试代码结果的数组,expected_items是一个包含预期项目的数组(按任意顺序)。

要确保所有项目都存在(并且不存在额外的项目),请结合以上内容并检查数组长度:

assert_equal expected_items.length, test_result_items.length

请注意,如果项目是唯一的,这将只确定两个数组是否相等。 (因为与test_result_items['a', 'a', 'a']确实只有expected_items['a', 'b', 'c']中存在的物品。)

© www.soinside.com 2019 - 2024. All rights reserved.