比较 C# 单元测试中的两个 List<string[]> 对象



>我正在尝试创建一个单元测试来比较两个字符串数组列表。

我尝试创建两个完全相同的List<string[]>对象,但是当我使用CollectionAssert.AreEqual(expected, actual);时,测试失败:

[TestMethod]
public void TestList()
{
List<string[]> expected = new List<string[]> {
new string[] { "John", "Smith", "200" },
new string[] { "John", "Doe", "-100" }
};
List<string[]> actual = new List<string[]> {
new string[] { "John", "Smith", "200" },
new string[] { "John", "Doe", "-100" }
};
CollectionAssert.AreEqual(expected, actual);
}

我也试过Assert.IsTrue(expected.SequenceEqual(actual));,但这也失败了。

如果我比较两个字符串列表或两个字符串数组,这两种方法都有效,但在比较两个字符串数组列表时它们不起作用。

我假设这些方法失败了,因为它们比较了两个对象引用列表而不是数组字符串值。

如何比较这两个List<string[]>对象并判断它们是否真的相同?

它失败了,因为列表中的项目是对象(string[]),并且由于您没有指定CollectionAssert.AreEqual应该如何比较两个序列中的元素,因此它回退到默认行为,即比较引用。例如,如果要将列表更改为以下内容,您会发现测试通过,因为现在两个列表都引用相同的数组:

var first = new string[] { "John", "Smith", "200" };
var second = new string[] { "John", "Smith", "200" };
List<string[]> expected = new List<string[]> { first, second};
List<string[]> actual = new List<string[]> { first, second};

为了避免引用比较,您需要告诉CollectionAssert.AreEqual如何比较元素,您可以通过在调用元素时传入IComparer来做到这一点:

CollectionAssert.AreEqual(expected, actual, StructuralComparisons.StructuralComparer);

CollectionAssert.AreEqual(expected, actual);失败,因为它比较对象引用。expectedactual指的是不同的对象。

Assert.IsTrue(expected.SequenceEqual(actual));失败的原因相同。这次比较了expectedactual的内容,但元素本身是不同的数组引用。

也许尝试使用 SelectMany(选择)来展平这两个序列:

var expectedSequence = expected.SelectMany(x => x).ToList();
var actualSequence = actual.SelectMany(x => x).ToList();
CollectionAssert.AreEqual(expectedSequence, actualSequence);

正如Enigmatity在他的评论中正确指出的那样,当数组和/或其元素的数量不同时,SelectMany可能会给出积极的结果,但是扁平化列表将导致相同数量的元素。只有在这些数组中始终具有相同数量的数组和元素的情况下,它才是安全的。

最好的解决方案是检查每个子集合中的项目以及每个子集合中的项目数。

试试这个:

bool equals = expected.Count == actual.Count &&
Enumerable.Range(0, expected.Count).All(i => expected[i].Length == actual[i].Length &&
expected[i].SequenceEqual(actual[i]));
Assert.IsTrue(equals);

这将检查:

  • 两个列表的长度相同
  • 两个列表中的所有子集合对都具有相同的长度
  • 每对子集合中的项相同

注意:使用SelectMany不是一个好主意,因为它可能会产生误报,您的第二个列表中有相同的项目,但分散在不同的子集合中。我的意思是,即使第二个列表在单个子集合中具有相同的项目,它也会认为两个列表是相同的。

相关内容