如何断言两个列表包含在 NUnit 中具有相同公共属性的元素? [英] How to assert that two list contains elements with the same public properties in NUnit?
问题描述
我想断言两个列表的元素包含我期望的值,例如:
I want to assert that the elements of two list contains values that I expected, something like:
var foundCollection = fooManager.LoadFoo();
var expectedCollection = new List<Foo>()
{
new Foo() { Bar = "a", Bar2 = "b" },
new Foo() { Bar = "c", Bar2 = "d" }
};
//assert: I use AreEquivalent since the order does not matter
CollectionAssert.AreEquivalent(expectedCollection, foundCollection);
但是,上面的代码不起作用(我猜是因为 .Equals() 对于具有相同值的不同对象不会返回 true).在我的测试中,我只关心公共属性值,而不关心对象是否相等.我能做些什么来做出我的断言?
However the above code will not work (I guess because .Equals() does not return true for different objects with the same value). In my test, I only care about the public property values, not whether the objects are equal. What can I do to make my assertion?
推荐答案
REWORKED ANSWER
有一个 CollectionAssert.AreEqual(IEnumerable, IEnumerable, IComparer)
重载来断言两个集合以相同的顺序包含相同的对象,使用 IComparer
实现来检查对象等价性.
There is a CollectionAssert.AreEqual(IEnumerable, IEnumerable, IComparer)
overload to assert that two collections contain the same objects in the same order, using an IComparer
implementation to check the object equivalence.
在上述场景中,顺序并不重要.然而,为了充分处理两个集合中有多个等价对象的情况,有必要首先对每个集合中的对象进行排序,并使用一一比较来确保等价对象的数量也相同在两个集合中.
In the scenario described above, the order is not important. However, to sufficiently handle also the situation where there are multiple equivalent objects in the two collections, it becomes necessary to first order the objects in each collection and use one-by-one comparison to ensure that also the number of equivalent objects are the same in the two collections.
Enumerable.OrderBy
提供了一个带有 IComparer
参数的重载.为了确保两个集合以相同的顺序排序,或多或少需要标识属性的类型实现IComparable
.这是一个比较器类的示例,它实现了 IComparer
和 IComparer
接口,并且假定 Bar
优先订购时:
Enumerable.OrderBy
provides an overload that takes an IComparer<T>
argument. To ensure that the two collections are sorted in the same order, it is more or less required that the types of the identifying properties implement IComparable
. Here is an example of a comparer class that implements both the IComparer
and IComparer<Foo>
interfaces, and where it is assumed that Bar
takes precedence when ordering:
public class FooComparer : IComparer, IComparer<Foo>
{
public int Compare(object x, object y)
{
var lhs = x as Foo;
var rhs = y as Foo;
if (lhs == null || rhs == null) throw new InvalidOperationException();
return Compare(lhs, rhs);
}
public int Compare(Foo x, Foo y)
{
int temp;
return (temp = x.Bar.CompareTo(y.Bar)) != 0 ? temp : x.Bar2.CompareTo(y.Bar2);
}
}
要断言两个集合中的对象相同并且数量相等(但不一定以相同的顺序开始),以下几行应该可以解决问题:
To assert that the objects in the two collections are the same and comes in equal numbers (but not necessarily in the same order to begin with), the following lines should do the trick:
var comparer = new FooComparer();
CollectionAssert.AreEqual(
expectedCollection.OrderBy(foo => foo, comparer),
foundCollection.OrderBy(foo => foo, comparer), comparer);
这篇关于如何断言两个列表包含在 NUnit 中具有相同公共属性的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!