Assert.Equals() never calls
Equals()
operator ==
operator !=
Am I missing something? I have implemented IEquatable but still the methods are never being called while using nunit.
if (objectA != objectB) Assert.Fail(); //doesnt fail
if (!objectA.Equals(objectB)) Assert.Fail(); //doesnt fail
Assert.AreEqual(objectA, objectB); //fail
UPDATE
I should have been clearer.
public class Entity
{
public int ID { get; set; }
}
var objectA = new Entity() { ID = 1 };
var objectB = new Entity() { ID = 1 };
two separate instances both with the same ID I have implemented all the relevant methods to make this work for ==, != and Equals but nunit AreSame and AreEqual still fails to call those methods.
The assert. equal() method tests if two values are equal, using the == operator. If the two values are not equal, an assertion failure is being caused, and the program is terminated. To compare the values using the === operator, use the assert. strictEqual() method.
Testing Object Equivalence These methods test whether the same objects are referenced by the two arguments. Assert. AreSame( object expected, object actual ); Assert. AreSame( object expected, object actual, string message ); Assert.
Asserts that a condition is true. If the condition is false the method throws an AssertionException. Asserts that a condition is true. If the condition is false the method throws an AssertionException.
NUnit is able to compare single-dimensioned arrays, multi-dimensioned arrays, nested arrays (arrays of arrays) and collections. Two arrays or collections are considered equal if they have the same dimensions and if each pair of corresponding elements is equal.
Use Assert.AreEqual(a, b)
for value types, Assert.AreSame(a, b)
for reference types. http://www.nunit.org/index.php?p=identityAsserts&r=2.2.7
You are definitely correct. I was wrestling with a similar problem earlier today, until I found your post and am now sure, that NUnit IsEqualTo() does not consistently call the Equals overrides provided.
I say consistently, because sometimes it does. As a matter of fact I have two classes. The second one derived from the first. When I call Is.EqualTo() on instances of the first, NUnit calls the Equals overrides, for instances of the second it does not.
While that is very peculiar, I have no time to investigate further into what is going on.
People with similar problems or solutions should definitely post about it, as this is a very annoying thing and actually had me doubt the validity of my tests.
In the meantime I created the following Affirm class, which calls the Equals overrides for sure (I checked it). It uses NUnit to do a simple equality Assert instead of Is.EqualTo() and somewhat remedies the fact, that this way NUnit doesn't give string representations of the objects in case the test fails.
So here it is:
using NUnit.Framework;
public static class Affirm
{
public static Affirmer That(object actual)
{
return new Affirmer(actual);
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public class Affirmer
{
readonly object _actual;
public Affirmer(object actual)
{
_actual = actual;
}
public void IsEqualTo(object expected)
{
string failureMessage = string.Format("\nExpected: <{0}>\nBut was: <{1}>", _actual, expected);
Assert.That(_actual.Equals(expected), Is.True, failureMessage);
}
public void IsNotEqualTo(object expected)
{
string failureMessage = string.Format("\nDid not excpect: <{0}>\nBut was: <{1}>", _actual, expected);
Assert.That(_actual.Equals(expected), Is.False, failureMessage);
}
}
Use it like this:
Affirm.That(actualObject).IsEqualTo(expectedObject);
and
Affirm.That(actualObject).IsNotEqualTo(expectedObject);
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With