Search code examples
c#unit-testingequalitymbunitgallio

Comparing two objects that are the same in 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?


Solution

  • 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;
            }