Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit Assert.Equals What am I missing?

Tags:

nunit

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.

like image 475
bleevo Avatar asked Jul 06 '09 03:07

bleevo


People also ask

What is Assert equal?

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.

How do I compare two objects in NUnit?

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.

How do I use Assert in NUnit?

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.

Is equal NUnit?

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.


2 Answers

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

like image 118
Robert Avatar answered Dec 16 '22 04:12

Robert


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.

like image 44
Thorsten Lorenz Avatar answered Dec 16 '22 03:12

Thorsten Lorenz