[ACCEPTED]-NUnit Assert.Equals What am I missing?-nunit
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
0
You are definitely correct. I was wrestling 23 with a similar problem earlier today, until I found your 22 post and am now sure, that NUnit IsEqualTo() does 21 not consistently call the Equals overrides 20 provided.
I say consistently, because sometimes 19 it does. As a matter of fact I have two 18 classes. The second one derived from the 17 first. When I call Is.EqualTo() on instances 16 of the first, NUnit calls the Equals overrides, for 15 instances of the second it does not.
While 14 that is very peculiar, I have no time to 13 investigate further into what is going on.
People 12 with similar problems or solutions should 11 definitely post about it, as this is a very 10 annoying thing and actually had me doubt 9 the validity of my tests.
In the meantime 8 I created the following Affirm class, which 7 calls the Equals overrides for sure (I checked 6 it). It uses NUnit to do a simple equality 5 Assert instead of Is.EqualTo() and somewhat 4 remedies the fact, that this way NUnit doesn't 3 give string representations of the objects 2 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 1 it like this:
Affirm.That(actualObject).IsEqualTo(expectedObject);
and
Affirm.That(actualObject).IsNotEqualTo(expectedObject);
Hope this helps.
Some frameworks allow for equality to work 11 differently before the Id is assigned (ie, the 10 Entity is unsaved) than afterwarsd, when 9 its clear that the intent is that the Entity 8 Id is the sole basis for quality. Are you 7 using some sort of framework or is Entity 6 your own class?
If it's your own class can 5 you show the gist of your Equals() logic?
Cheers, Berryl
FYI 4 Assert.AreSame is NEVER a test to validate 3 your implementation of IEquatable! See ReferenceEquals 2 in your help doc to understand that assertion 1 better.
It should work (see this related question) if the Equals 7 method was overridden correctly. Could it 6 be a problem with your Equals method (although 5 if it simply consists of int comparison 4 I would think not)? Might be worth setting 3 a break point in your Equals method and 2 then running the test to see what's going 1 on behind the scenes.
You might want to check out this question: NUnit's Assert.Equals throws exception "Assert.Equals should not be used for assertions"
tl;dr;
Assert.Equals(obj1, obj2)
is 2 overridden by NUnit, and throws an exception. You 1 should use Assert.AreEqual(obj1, obj2)
instead.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.