Compare Equality Between Two Objects in Nunit

Compare equality between two objects in NUnit

Override .Equals for your object and in the unit test you can then simply do this:

Assert.AreEqual(LeftObject, RightObject);

Of course, this might mean you just move all the individual comparisons to the .Equals method, but it would allow you to reuse that implementation for multiple tests, and probably makes sense to have if objects should be able to compare themselves with siblings anyway.

How to compare complex objects with NUnit Unit Test

There is a tool used in unit testing called Fluent Assertions which is capable of doing such comparisons.

Note however

Objects are equivalent when both object graphs have equally named
properties with the same value, irrespective of the type of those
objects. Two properties are also equal if one type can be converted to
another and the result is equal. The type of a collection property is
ignored as long as the collection implements
System.Collections.IEnumerable and all items in the collection are
structurally equal. Notice that actual behavior is determined by the
global defaults managed by FluentAssertions.AssertionOptions.

using FluentAssertions;

//...

// Example 1
AMockObject a = new AMockObject();
AMockObject b = new AMockObject();
a.ShouldBeEquivalentTo(b); // Asserts that an object is equivalent to another object.

// Example 2
AMockObject a = new AMockObject() { Id = 1 };
AMockObject b = new AMockObject() { Id = 1 };
a.ShouldBeEquivalentTo(b); //Asserts that an object is equivalent to another object.

// Example 3
AMockObject a = new AMockObject() { Id = 1 };
a.Numbers.Add(1);
a.Numbers.Add(2);
a.Numbers.Add(3);
AMockObject b = new AMockObject() { Id = 1 };
b.Numbers.Add(1);
b.Numbers.Add(2);
b.Numbers.Add(3);
a.ShouldBeEquivalentTo(b)
a.Numbers.ShouldAllBeEquivalentTo(b.Numbers); // Asserts that a collection of objects is equivalent to another collection of objects.

Documentation here

Nunit: Check that two objects are the same

From nunit documentation,

When checking the equality of user-defined classes, NUnit makes use of the Equals override on the expected object. If you neglect to override Equals, you can expect failures non-identical objects. In particular, overriding operator== without overriding Equals has no effect.

You can however supply your own comparer to test if the values are equal for the properties.

If the default NUnit or .NET behavior for testing equality doesn't meet your needs, you can supply a comparer of your own through the Using modifier. When used with EqualConstraint, you may supply an IEqualityComparer, IEqualityComparer, IComparer, IComparer; or Comparison as the argument to Using.

Assert.That( myObj1, Is.EqualTo( myObj2 ).Using( myComparer ) );

So in this case your comparer would be

    public class FooComparer : IEqualityComparer
{
public bool Equals(Foo x, Foo y)
{
if (x.A == y.A && x.B == y.B)
{
return true;
}
return false;
}
public int GetHashCode(Foo inst)
{
return inst.GetHashCode();
}
}

Can I compare objects of different types using NUnit's EqualTo().Using()?

After further investigation, I found that the correct behaviour was already implemented, just not in the default EqualityAdapter. Using any of the generic adapters works. That is, changing the comparer in the question to this makes the tests pass:

public class DateTimeComparer : IComparer<IConvertible>
{
public static readonly DateTimeComparer Instance = new DateTimeComparer();

public int Compare(IConvertible x, IConvertible y)
{
var dateTime1 = Convert.ToDateTime(x);
var dateTime2 = Convert.ToDateTime(y);
return dateTime1.CompareTo(dateTime2);
}
}

NUnit comparing two lists

I convert my comment to answer on request.

Well, this fails because AreEqual uses reference comparison. In order to make it work you need value comparison(your own custom comparison).

You can pretty much do that by implementing IEquatable interface. and keep in mind when you're implementing this interface you must override Object.Equals and Object.GetHashCode as well to get consistent results.

.Net framework supports doing this without implementing IEquatable you need IEqualityComparer that should do the trick, but nunit should have a method which takes this as a overload. Am not certain about "nunit" though.

NUnit Assert.AreEqual(LeftObject,RigtObject)

well as the threads you linked to suggest, Assert.AreEqual compares as a reference equal unless you override it.

So even though everything looks the same, they're probably different object / references.

You could have a look at the PropertyValuesAreEquals from this answer: https://stackoverflow.com/a/318238/919324 and either compare your objects like that, or override the comparison of the objects



Related Topics



Leave a reply



Submit