NUnit Assert.Equals 我错过了什么? [英] NUnit Assert.Equals What am I missing?
问题描述
Assert.Equals() 从不调用
Assert.Equals() never calls
Equals()
operator ==
operator !=
我错过了什么吗?我已经实现了 IEquatable,但在使用 nunit 时仍然没有调用这些方法.
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
更新
我应该更清楚.
public class Entity
{
public int ID { get; set; }
}
var objectA = new Entity() { ID = 1 };
var objectB = new Entity() { ID = 1 };
两个独立的实例都具有相同的 ID 我已经实现了所有相关的方法来使其适用于 ==、!= 和 Equals,但是 nunit AreSame 和 AreEqual 仍然无法调用这些方法.
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.
推荐答案
你说得对.我正在与 类似问题 今天早些时候,直到我找到你的帖子,现在我确信,NUnit IsEqualTo() 并没有始终如一地调用提供的 Equals 覆盖.
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.
我一直说,因为有时确实如此.事实上,我有两个班级.第二个是从第一个衍生出来的.当我在第一个实例上调用 Is.EqualTo() 时,NUnit 调用 Equals 覆盖,对于第二个实例则不会.
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.
与此同时,我创建了以下 Affirm 类,它肯定会调用 Equals 覆盖(我已检查过).它使用 NUnit 执行简单的相等 Assert 而不是 Is.EqualTo() 并在一定程度上弥补了这样一个事实,即如果测试失败,NUnit 不会给出对象的字符串表示.
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.
这里是:
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);
}
}
像这样使用它:
Affirm.That(actualObject).IsEqualTo(expectedObject);
和
Affirm.That(actualObject).IsNotEqualTo(expectedObject);
希望这会有所帮助.
这篇关于NUnit Assert.Equals 我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!