Helps testing whether the equality members of your class are implemented correctly. If you do not implement any of these, this package is not for you.
However, if you implement one of the below methods, EqualityAssert tests all of them for correctness. Another reason for me to write this was to increase code coverage of tests, when the equality members are often generated by tools like Resharper.
public bool Equals(object other);
public int GetHashCode();
public bool Equals<T>(T other);
public static bool operator== (YourClass first, YourClass second);
public static bool operator!= (YourClass first, YourClass second);
as well as the members of classes implementing IEqualityComparer
public bool Equals(T first, T second);
public bool GetHashCode(T obj);
The Equals() and operator methods are called in the important combinations, and it is asserted, that their result is correct. The values tried are the instances first
, second
and third
, NULL
(or default(T)
), and for Equals(object)
also an instance of object
.
Download the nuget package EqualityAssert using the nuget package manager or the console:
Install-Package MiP.EqualityAssertion
In your test, create three different instances of your class, where the first and second must be value-equal (.Equals()
should return true).
Then call EqualityAssert.EqualityMembers(first, second, third)
. If an issue with the implementation is found, an AssertionException
is raised.
MyClass first = new MyClass(1,"one");
MyClass second = new MyClass(1,"one");
MyClass third = new MyClass(3,"three");
EqualityAssert.EqualityMembers(first, second, third);