NUnit Assert.Equals 我错过了什么?
Assert.Equals() 永远不会调用
Equals()
operator ==
operator !=
我错过了什么吗? 我已经实现了 IEquatable,但在使用 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 仍然无法调用这些方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于值类型使用
Assert.AreEqual(a, b)
,对于引用类型使用Assert.AreSame(a, b)
。 http://www.nunit.org/index.php?p =identityAsserts&r=2.2.7Use
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你绝对是对的。
我正在与 类似的问题进行斗争今天早些时候的问题,直到我找到你的帖子,现在我确信,NUnit IsEqualTo() 并没有一致地调用提供的 Equals 覆盖。
我说始终如一,因为有时确实如此。 事实上我有两节课。 第二个是从第一个衍生出来的。 当我在第一个实例上调用 Is.EqualTo() 时,NUnit 会调用 Equals 覆盖,而对于第二个实例则不会。
虽然这很奇怪,但我没有时间进一步调查到底发生了什么。
有类似问题或解决方案的人绝对应该发布它,因为这是一件非常烦人的事情,实际上让我怀疑我的测试的有效性。
与此同时,我创建了以下 Affirm 类,它肯定会调用 Equals 覆盖(我检查过)。 它使用 NUnit 来执行简单的相等断言,而不是 Is.EqualTo(),并在一定程度上弥补了这样一个事实:这样,在测试失败的情况下,NUnit 不会给出对象的字符串表示形式。
所以这里是:
像这样使用它:
希望
这会有所帮助。
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:
Use it like this:
and
Hope this helps.
一些框架允许平等在分配 Id 之前(即实体未保存)与之后不同地工作,当很明显意图是实体 Id 是质量的唯一基础时。 您是否使用某种框架或者 Entity 是您自己的类?
如果这是你自己的类,你能展示你的 Equals() 逻辑的要点吗?
干杯,
Berryl
FYI Assert.AreSame 从来都不是验证 IEquatable 实现的测试! 请参阅帮助文档中的 ReferenceEquals 以更好地理解该断言。
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.
如果 Equals 方法为正确覆盖。 这可能是您的 Equals 方法的问题(尽管如果它只是由 int 比较组成,我认为不是)? 可能值得在 Equals 方法中设置一个断点,然后运行测试以查看幕后发生的情况。
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.
您可能想查看这个问题:
NUnit 的 Assert.Equals 抛出异常“Assert.Equals 不应该用于断言”
tl;dr;
Assert.Equals(obj1, obj2)
被 NUnit 覆盖,并引发异常。 您应该使用Assert.AreEqual(obj1, obj2)
来代替。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 useAssert.AreEqual(obj1, obj2)
instead.