比较 MbUnit 中相同的两个对象
从 MBUnit 中,我尝试使用以下命令检查两个对象的值是否相同,
Assert.AreSame(RawDataRow, result);
但出现以下错误:
Expected Value & Actual Value : {RawDataRow: CentreID = "CentreID1",
CentreLearnerRef = "CentreLearnerRef1",
ContactID = 1, DOB = 2010-05-05T00:00:00.0000000,
Email = "Email1", ErrorCodes = "ErrorCodes1",
ErrorDescription = "ErrorDescription1", FirstName = "FirstName1"}
备注:格式化时两个值看起来相同,但它们是不同的实例。
我不想遍历每个属性。我可以从 MbUnit 执行此操作吗?
From MBUnit I am trying to check if the values of two objects are the same using
Assert.AreSame(RawDataRow, result);
However I am getting the following fail:
Expected Value & Actual Value : {RawDataRow: CentreID = "CentreID1",
CentreLearnerRef = "CentreLearnerRef1",
ContactID = 1, DOB = 2010-05-05T00:00:00.0000000,
Email = "Email1", ErrorCodes = "ErrorCodes1",
ErrorDescription = "ErrorDescription1", FirstName = "FirstName1"}
Remark : Both values look the same when formatted but they are distinct instances.
I don't want to have to go through each property. Can I do this from MbUnit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上,
Assert.AreEqual
使用Object.Equals()
来验证实际实例与预期实例之间的相等性,而Assert.AreSame
使用 <代码>Object.ReferenceEquals。如果你的类没有实现任何内置的平等机制;例如,通过覆盖
Object.Equals
,您最终会遇到您所描述的问题,因为 MbUnit 不知道如何比较被测类型的两个实例。有几种解决方案。其中之一是 Coppermill 的解决方案:您可能希望实现基于反射的结构相等比较器。但 MbUnit 已经有一个内置的类似的功能。它称为
StructuralEqualityComparer
并且非常易于使用。那么为什么要重新发明轮子呢?无论如何,我建议您阅读该文章: http:// /interfaceingreality.blogspot.com/2009/09/equality-assertions-in-mbunit-v3.html
您可能还想阅读Gallio wiki 中的那篇文章。
Basically,
Assert.AreEqual
usesObject.Equals()
to verify the equality between the actual and the expected instance, whileAssert.AreSame
usesObject.ReferenceEquals
.If your class does not implement any built-in equality mechanism; for example by overriding
Object.Equals
, you will end up with the issue you describe because MbUnit does not know how to compare two instances of the type under test.There are several solutions. One of them is Coppermill's solution: you may want to implement a structural equality comparer based on reflection. But MbUnit has already a built-in feature like that. It's called
StructuralEqualityComparer<T>
and it's very easy to use. So why reinventing the wheel?Anyway, I suggest you read that article: http://interfacingreality.blogspot.com/2009/09/equality-assertions-in-mbunit-v3.html
You may also want to read that article in the Gallio wiki.
您是否专门寻找引用相等?
AreSame
正在测试两个引用是否都指向同一个对象。来自文档:如果您只是想比较对象的两个实例在逻辑上是否相等,那么
AreEqual
就是您所需要的。Are you specifically looking for referential equality?
AreSame
is testing that both references point to the same object. From the doco:If you are simply looking to compare that two instances of an object are logically equal, then
AreEqual
is what you need.我最终使用 Reflections 构建了自己的
I've ended up building my own using Reflections
在 MbUnit 3 中,您可以使用 StructuralEqualityComparer:
Assert.AreElementsEqual(obj1,obj2, new StructuralEqualityComparer());
In MbUnit 3 you can use StructuralEqualityComparer:
Assert.AreElementsEqual(obj1,obj2, new StructuralEqualityComparer());