MSTest的:CollectionAssert.AreEquivalent失败。预期集合包含1发生(S) [英] MSTest: CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of
问题描述
问题
谁能告诉我为什么我的单元测试失败,出现此错误信息?
Can anyone tell me why my unit test is failing with this error message?
CollectionAssert.AreEquivalent失败。预期集合包含1 发生(县)。实际 集合包含0出现(S)。
CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of . The actual collection contains 0 occurrence(s).
目标
我要检查,如果两个列表是相同的。它们是相同的,如果两者都包含具有相同的属性值相同的元件。顺序是无关紧要的。
I'd like to check if two lists are identical. They are identical if both contain the same elements with the same property values. The order is irrelevant.
code例如
这是产生错误的code。 的List1
和 list2中
是相同的,即对方的复制 - 粘贴。
This is the code which produces the error. list1
and list2
are identical, i.e. a copy-paste of each other.
[TestMethod]
public void TestListOfT()
{
var list1 = new List<MyPerson>()
{
new MyPerson()
{
Name = "A",
Age = 20
},
new MyPerson()
{
Name = "B",
Age = 30
}
};
var list2 = new List<MyPerson>()
{
new MyPerson()
{
Name = "A",
Age = 20
},
new MyPerson()
{
Name = "B",
Age = 30
}
};
CollectionAssert.AreEquivalent(list1.ToList(), list2.ToList());
}
public class MyPerson
{
public string Name { get; set; }
public int Age { get; set; }
}
我也试过这条线(<一href="http://stackoverflow.com/questions/2441188/mstest-collectionassert-use-with-generics/2441448#2441448">source)
CollectionAssert.AreEquivalent(list1.ToList(), list2.ToList());
和这条线(<一href="http://stackoverflow.com/questions/662458/unittesting-ilist-with-collectionassert/662474#662474">source)
CollectionAssert.AreEquivalent(list1.ToArray(), list2.ToArray());
P.S。
相关堆栈溢出的问题:
我已经看到了这两个问题,但答案没有帮助。
I've seen both these questions, but the answers didn't help.
- CollectionAssert use with generics?
- Unittesting IList with CollectionAssert
推荐答案
您是绝对正确的。除非你提供的东西像的IEqualityComparer&LT; MyPerson&GT;
或实施 MyPerson.Equals()
,两个 MyPerson
对象将与的Object.Equals
,就像任何其他对象。由于对象是不同的,断言将会失败。
You are absolutely right. Unless you provide something like an IEqualityComparer<MyPerson>
or implement MyPerson.Equals()
, the two MyPerson
objects will be compared with object.Equals
, just like any other object. Since the objects are different, the Assert will fail.
这篇关于MSTest的:CollectionAssert.AreEquivalent失败。预期集合包含1发生(S)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!