CollectionAssert.Contains(myList, myItem) != Assert.IsTrue(myList.Contains(myItem))
我一直在考虑为控制器控制器实现单元测试,特别是围绕测试集合。在 MSDN 示例 上使用 CollectionAssert.Contains()
确认对象是否出现在列表中。
我有一个 List
,其中 myObject
实现 IEquatable
(即实现 Equals()
,以便List
能够正确辨别列表中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看
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 withobject.Equals(current, target)
.So the answer to your question is that if you haven't overridden the
object
version ofEquals()
so that it dispatches to yourIEquatable<T>
version, you should. Otherwise the test is going to fail if reference equality isn't satisfied, since theIEquatable<T>
overload ofEquals()
doesn't override the overload inherited fromobject
.看起来 CollectionAssert.Contains 并没有查看对象的成员值,而是比较实例的内存地址。以下示例的 CollectionAssert 将失败,因为虽然 obj 和 obj2 具有相同的成员值,但它们不是同一个实例对象;
话虽如此,以下示例的 CollectionAssert 将会成功,因为它将搜索 obj2,而不是任何等于 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;
That being said, the following example's CollectionAssert will succeed because it will be searching for obj2, and not any object that equals 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