Comparing two objects the same in MbUnit

From MBUnit I'm trying to check if the values ​​of two objects are the same with

Assert.AreSame(RawDataRow, result);

However, I get the following crash:

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"}

Note. Both values ​​look the same when formatted, but they are different instances.

I do not want to go through each property. Can I do this from MbUnit?

+3
source share
4 answers

I ended up making my own use of 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;
        }
0
source

In principle, it Assert.AreEqualuses Object.Equals()to verify the equality between the actual and the expected instance, while it Assert.AreSameuses Object.ReferenceEquals.

; , Object.Equals, , MbUnit , .

. Coppermill: , . MbUnit . StructuralEqualityComparer<T>, . ?

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://interfacingreality.blogspot.com/2009/09/equality-assertions-in-mbunit-v3.html

Gallio.

+2

? AreSame , . doco:

Assert.AreSame - , .

If you just want to compare that two instances of an object are logically equal, then AreEqualthis is what you need.

Assert.AreEqual - Checks that the actual value is equal to some expected value.

0
source

In MbUnit 3, you can use StructuralEqualityComparer:

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

0
source

Source: https://habr.com/ru/post/1744492/


All Articles