[英]Compare two List<string[]> objects in C# Unit Test
我正在尝试创建一个比较两个字符串数组列表的单元测试。
我尝试创建两个完全相同的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[]>
对象并判断它们是否真的相同?
CollectionAssert.AreEqual(expected, actual);
失败,因为它比较了对象引用。 expected
和actual
参考不同的对象。
Assert.IsTrue(expected.SequenceEqual(actual));
由于同样的原因失败了。 这次比较了expected
和actual
的内容,但元素本身就是不同的数组引用。
也许尝试使用SelectMany来压缩两个序列:
var expectedSequence = expected.SelectMany(x => x).ToList();
var actualSequence = actual.SelectMany(x => x).ToList();
CollectionAssert.AreEqual(expectedSequence, actualSequence);
由于Enigmativity在他的评论中正确注意到,当数组和/或它们的元素数量不同时,SelectMany可能会给出肯定的结果,但是对列表进行展平将导致相同数量的元素。 只有在这些数组中始终具有相同数量的数组和元素时才是安全的。
它失败了,因为列表中的项是对象( 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);
最好的解决方案是检查每个子集合中的项目以及每个相应子集合中的项目数。
试试这个:
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
不是一个好主意,因为它可能会产生误报,您在第二个列表中有相同的项目,但在不同的子集合中展开。 我的意思是,即使第二个列表在一个子集合中具有相同的项目,它也会认为两个列表是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.