NUnit Assert.Equals What am I missing?

走远了吗. 提交于 2019-12-10 01:57:08

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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 overridden by NUnit, and throws an exception. You should use Assert.AreEqual(obj1, obj2) instead.



来源:https://stackoverflow.com/questions/1085278/nunit-assert-equals-what-am-i-missing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!