tags:

views:

1062

answers:

4

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.

+2  A: 

Use AreEqual for value types, AreSame for reference types. http://www.nunit.org/index.php?p=identityAsserts&r=2.2.7

Robert
This still says they are not the same for some reason.. (it wont call my equal methods) :(What I am trying to test is 2 instances of the same class with the same ID that should say yes they are equal.
bleevo
Not sure, you could certainly do Assert.IsTrue(objectA == objectB) but I'm not sure why you need to.
Robert
+2  A: 

Some frameworks allow for equality to work differently before the Id is assigned (ie, the Entity is unsaved) than afterwarsd, when its clear that the intent is that the Entity Id is the sole basis for quality. Are you using some sort of framework or is Entity your own class?

If it's your own class can you show the gist of your Equals() logic?

Cheers, Berryl

FYI Assert.AreSame is NEVER a test to validate your implementation of IEquatable! See ReferenceEquals in your help doc to understand that assertion better.

Berryl
+1  A: 

It should work (see this related question) if the Equals method was overridden correctly. Could it be a problem with your Equals method (although if it simply consists of int comparison I would think not)? Might be worth setting a break point in your Equals method and then running the test to see what's going on behind the scenes.

jpoh
I think setting breakpoint was exactly what the OP was doing
zcrar70
+2  A: 

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.

Thorsten Lorenz
Just for completeness: As can be seen in your original post (http://stackoverflow.com/questions/1624848/does-nunits-is-equalto-not-work-reliably-for-classes-derived-from-generic-classe), your problem was caused by a bug in your override of object.Equals (object).(I guess this thread's OP's problem was caused by a similar bug.)
Fabian Schmied