XUnit Assertion for checking equality of objects

后端 未结 5 866
小蘑菇
小蘑菇 2021-02-03 19:11

I am using XUnit framework to test my C# code.

Is there any assert method available in this framework which does the object comparison? My intention is to check for equa

5条回答
  •  遇见更好的自我
    2021-02-03 19:47

    You need to have a custom comparer to achieve this, when you compare objects otherwise they are checked on the basis of whether they are referring to the same object in memory. To override this behavior you need to override the Equals and GetHashCode method and then you could do:

    Assert.True(obj1.Equals(obj2));
    

    Here is an MSDN page abt overloading Equals method: http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

    Also apt the comment on the question: What's the difference between IEquatable and just overriding Object.Equals()?

提交回复
热议问题