CollectionAssert.Contains(myList, myItem) != Assert.IsTrue(myList.Contains(myItem))

发布于 2024-12-05 15:33:10 字数 644 浏览 0 评论 0原文

我一直在考虑为控制器控制器实现单元测试,特别是围绕测试集合。在 MSDN 示例 上使用 CollectionAssert.Contains() 确认对象是否出现在列表中。

我有一个 List,其中 myObject 实现 IEquatable (即实现 Equals(),以便List.Contains() 能够正确辨别列表中 myObject 类型的对象是否存在

。然而,CollectionAssert.Contains()(用于 MS-VS 测试,而不是 nunit)函数似乎并未调用 Equals()。 所以我想知道它是否适用于简单数组? 如果不是,它如何能够比较自定义对象?

我只是将本例中的断言更改为 Assert.IsTrue(myList.Contains(myObjectInstance))

I have been looking at implementing unit tests for a controller controller, specifically around testing collections. On the MSDN example the use of CollectionAssert.Contains() confirms whether an object appears in a list.

I have a List<myObject> where myObject implements IEquatable (i.e. implementing a Equals(), so that the List<myObject>.Contains() is able to correctly discern the existence (or non-existence of an object of type myObject in the list).

The CollectionAssert.Contains() (for an MS-VS test, not nunit) function however, does not appear to call Equals().
So I'm wondering if it's for use on simple arrays?
If not, how is it able to compare custom objects?

I've simply changed my assert in this case to Assert.IsTrue(myList.Contains(myObjectInstance)).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-12-12 15:33:10

查看 CollectionAssert.Contains() 的代码,实际的比较是通过迭代集合并使用 object.Equals(current, target) 将每个元素与目标元素进行比较来完成的代码>.

因此,您的问题的答案是,如果您没有重写 Equals()object 版本,以便它分派到您的 IEquatable版本,你应该。否则,如果不满足引用相等性,测试将会失败,因为 Equals()IEquatable 重载不会覆盖从 对象

Looking at the code for CollectionAssert.Contains(), the actual comparison is done by iterating the collection, and comparing each element to the target elements with object.Equals(current, target).

So the answer to your question is that if you haven't overridden the object version of Equals() so that it dispatches to your IEquatable<T> version, you should. Otherwise the test is going to fail if reference equality isn't satisfied, since the IEquatable<T> overload of Equals() doesn't override the overload inherited from object.

甜味超标? 2024-12-12 15:33:10

看起来 CollectionAssert.Contains 并没有查看对象的成员值,而是比较实例的内存地址。以下示例的 CollectionAssert 将失败,因为虽然 obj 和 obj2 具有相同的成员值,但它们不是同一个实例对象

        MyType obj = new MyType { prop1 = "foo", prop2 = "bar" };
        MyType obj2 = new MyType { prop1 = "foo", prop2 = "bar" };

        List<MyType> lstObj = new List<MyType> { obj2 };

        CollectionAssert.Contains(lstObj, obj);

话虽如此,以下示例的 CollectionAssert 将会成功,因为它将搜索 obj2,而不是任何等于 obj2 的对象;

        MyType obj = new MyType { prop1 = "foo", prop2 = "bar" };
        MyType obj2 = new MyType { prop1 = "foo", prop2 = "bar" };

        List<MyType> lstObj = new List<MyType> { obj2 };

        CollectionAssert.Contains(lstObj, obj2);

换句话说,CollectionAssert.Contains 比较实例,而 List(T).Contains“通过使用默认相等比较器来确定相等性,如 T(列表中值的类型)的 IEquatable.Equals 方法的对象实现所定义。 ”

参考:http://msdn.microsoft.com/en-us/library/bhkz42b3 .aspx

It seems CollectionAssert.Contains is not looking at your object's member values, but is comparing instances' memory address. The following example's CollectionAssert will fail, due to the fact that, although they have the same member values, obj and obj2 are not the same instance object;

        MyType obj = new MyType { prop1 = "foo", prop2 = "bar" };
        MyType obj2 = new MyType { prop1 = "foo", prop2 = "bar" };

        List<MyType> lstObj = new List<MyType> { obj2 };

        CollectionAssert.Contains(lstObj, obj);

That being said, the following example's CollectionAssert will succeed because it will be searching for obj2, and not any object that equals obj2;

        MyType obj = new MyType { prop1 = "foo", prop2 = "bar" };
        MyType obj2 = new MyType { prop1 = "foo", prop2 = "bar" };

        List<MyType> lstObj = new List<MyType> { obj2 };

        CollectionAssert.Contains(lstObj, obj2);

In other words, CollectionAssert.Contains compares instances and List(T).Contains "determines equality by using the default equality comparer, as defined by the object's implementation of the IEquatable.Equals method for T (the type of values in the list)."

Reference : http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文