Skip to content

praschl/EqualityAssert

Repository files navigation

EqualityAssert

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.

Whats tested?

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.

How do I use it?

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.

Example code (yes RLY)

MyClass first = new MyClass(1,"one");
MyClass second = new MyClass(1,"one");
MyClass third = new MyClass(3,"three");
EqualityAssert.EqualityMembers(first, second, third);

Releases

No releases published

Packages

No packages published

Languages