Unit Test Assert.AreEqual failed

后端 未结 6 1402
别那么骄傲
别那么骄傲 2021-01-17 16:12

I have a unit test for a method which gets an object from a collection. This keeps failing and I cannot see why, so I have created a very simple test below to create 2 suppl

6条回答
  •  时光取名叫无心
    2021-01-17 16:33

    As every other answer says the issue is that you're trying to compare instances of Supplier [probably] without overriding Equals method. But I do not think you should override Equals for test purposes since it may affect production code or you may need another Equals logic in production code.

    Instead you should either assert each member one by one as you do it in first sample (if you do not have a lot of places where you want to compare entire object) or encapsulate this comparison logic in some class and use this class:

    static class SupplierAllFieldsComparer
    {
        public static void AssertAreEqual(Supplier expected, Supplier actual)
        {
            Assert.AreEqual(expected.SupplierID , actual.SupplierID );
            Assert.AreEqual(expected.SupplierName , actual.SupplierName );            
        }
    }
    

    // Test code:

    SupplierAllFieldsComparer.AssertAreEqual(expected, actual);
    

提交回复
热议问题