比较 MbUnit 中相同的两个对象

发布于 2024-08-31 15:45:45 字数 493 浏览 11 评论 0原文

从 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 技术交流群。

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

发布评论

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

评论(4

聊慰 2024-09-07 15:45:45

基本上,Assert.AreEqual 使用 Object.Equals() 来验证实际实例与预期实例之间的相等性,而 Assert.AreSame 使用 <代码>Object.ReferenceEquals。

如果你的类没有实现任何内置的平等机制;例如,通过覆盖 Object.Equals,您最终会遇到您所描述的问题,因为 MbUnit 不知道如何比较被测类型的两个实例。

有几种解决方案。其中之一是 Coppermill 的解决方案:您可能希望实现基于反射的结构相等比较器。但 MbUnit 已经有一个内置的类似的功能。它称为 StructuralEqualityComparer 并且非常易于使用。那么为什么要重新发明轮子呢?

Assert.AreSame(RawDataRow, result, new StructuralEqualityComparer<MyDataRow>
{
   { x => x.CentreID },
   { x => x.CentreLearnerRef, (a, b) => a.Equals(b, StringComparison.OrdinalIgnoreCase) },
   { x => x.ContactID },
   // You can customize completely the comparison process...
});

无论如何,我建议您阅读该文章: http:// /interfaceingreality.blogspot.com/2009/09/equality-assertions-in-mbunit-v3.html

您可能还想阅读Gallio wiki 中的那篇文章

Basically, Assert.AreEqual uses Object.Equals() to verify the equality between the actual and the expected instance, while Assert.AreSame uses Object.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?

Assert.AreSame(RawDataRow, result, new StructuralEqualityComparer<MyDataRow>
{
   { x => x.CentreID },
   { x => x.CentreLearnerRef, (a, b) => a.Equals(b, StringComparison.OrdinalIgnoreCase) },
   { x => x.ContactID },
   // You can customize completely the comparison process...
});

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.

忱杏 2024-09-07 15:45:45

您是否专门寻找引用相等AreSame 正在测试两个引用是否都指向同一个对象。来自文档:

Assert.AreSame - 验证实际值与某个预期值在参考上相同。

如果您只是想比较对象的两个实例在逻辑上是否相等,那么 AreEqual 就是您所需要的。

Assert.AreEqual - 验证实际值等于某个期望值。

Are you specifically looking for referential equality? AreSame is testing that both references point to the same object. From the doco:

Assert.AreSame - Verifies that an actual value is referentially identical to some expected value.

If you are simply looking to compare that two instances of an object are logically equal, then AreEqual is what you need.

Assert.AreEqual - Verifies that an actual value equals some expected value.

老子叫无熙 2024-09-07 15:45:45

我最终使用 Reflections 构建了自己的

private bool PropertiesEqual<T>(T object1, T object2)
        {
            PropertyDescriptorCollection object2Properties = TypeDescriptor.GetProperties(object1);
            foreach (PropertyDescriptor object1Property in TypeDescriptor.GetProperties(object2))
            {
                PropertyDescriptor object2Property = object2Properties.Find(object1Property.Name, true);

                if (object2Property != null)
                {
                    object object1Value = object1Property.GetValue(object1);
                    object object2Value = object2Property.GetValue(object2);

                    if ((object1Value == null && object2Value != null) || (object1Value != null && object2Value == null))
                    {
                        return false;
                    }

                    if (object1Value != null && object2Value != null)
                    {
                        if (!object1Value.Equals(object2Value))
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }

I've ended up building my own using Reflections

private bool PropertiesEqual<T>(T object1, T object2)
        {
            PropertyDescriptorCollection object2Properties = TypeDescriptor.GetProperties(object1);
            foreach (PropertyDescriptor object1Property in TypeDescriptor.GetProperties(object2))
            {
                PropertyDescriptor object2Property = object2Properties.Find(object1Property.Name, true);

                if (object2Property != null)
                {
                    object object1Value = object1Property.GetValue(object1);
                    object object2Value = object2Property.GetValue(object2);

                    if ((object1Value == null && object2Value != null) || (object1Value != null && object2Value == null))
                    {
                        return false;
                    }

                    if (object1Value != null && object2Value != null)
                    {
                        if (!object1Value.Equals(object2Value))
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
丿*梦醉红颜 2024-09-07 15:45:45

在 MbUnit 3 中,您可以使用 StructuralEqualityComparer:

Assert.AreElementsEqual(obj1,obj2, new StructuralEqualityComparer());

In MbUnit 3 you can use StructuralEqualityComparer:

Assert.AreElementsEqual(obj1,obj2, new StructuralEqualityComparer());

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