Assert.AreNotEqual 和 Assert.AreNotSame 有什么区别?
之间有什么区别
Assert.AreNotEqual
在C#中,和
Assert.AreNotSame
In C#, what's the difference between
Assert.AreNotEqual
and
Assert.AreNotSame
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里给出的几乎所有答案都是正确的,但可能值得举一个例子:
AreNotEqual
和AreNotSame
只是AreEqual
和的反转当然是相同的。
编辑:对当前接受的答案的反驳< /a>...
如果将
Assert.AreSame
与值类型一起使用,它们会被装箱。 换句话说,它相当于:firstNumber
和secondNumber
都没有对象值,因为int
是值类型。AreSame
调用失败的原因是在 .NET 中,对值进行装箱每次都会创建一个新框。 (在 Java 中有时不会 - 这让我以前很困惑。)基本上,在比较值类型时,永远不应该使用
AreSame
。 当您比较引用类型时,如果您想检查相同的引用,请使用AreSame
; 使用AreEqual
检查Equals
下的等价性。 编辑:请注意,在某些情况下,NUnit 不直接使用Equals
; 它具有对集合的内置支持,其中集合中的元素被测试是否相等。答案中的主张是:
完全取决于变量的初始化方式。 如果他们使用字符串文字,那么是的,实习会解决这个问题。 但是,如果您使用:,
则
AreSame
和AreEqual
几乎肯定不会返回相同的值。至于:
我几乎从不想要检查参考身份。 它对我来说很少有用。 我想检查等价,这就是
AreEqual
检查的内容。 (我并不是说AreSame
不应该存在 - 这是一个有用的方法,只是比AreEqual
少得多。)Almost all the answers given here are correct, but it's probably worth giving an example:
AreNotEqual
andAreNotSame
are just inversions ofAreEqual
andAreSame
of course.EDIT: A rebuttal to the currently accepted answer...
If you use
Assert.AreSame
with value types, they are boxed. In other words, it's equivalent to doing:Neither
firstNumber
norsecondNumber
has an object value, becauseint
is a value type. The reason theAreSame
call will fail is because in .NET, boxing a value creates a new box each time. (In Java it sometimes doesn't - this has caught me out before.)Basically you should never use
AreSame
when comparing value types. When you're comparing reference types, useAreSame
if you want to check for identical references; useAreEqual
to check for equivalence underEquals
. EDIT: Note that there are situations where NUnit doesn't just useEquals
directly; it has built-in support for collections, where the elements in the collections are tested for equality.The claim in the answer that:
entirely depends on how the variables are initialized. If they use string literals, then yes, interning will take care of that. If, however, you use:
then
AreSame
andAreEqual
will almost certainly not return the same value.As for:
I almost never want to check for reference identity. It's rarely useful to me. I want to check for equivalence which is what
AreEqual
checks for. (I'm not saying thatAreSame
shouldn't be there - it's a useful method, just far more rarely thanAreEqual
.)两个事物可以是相同的,但可以是不同的对象。 AreNotEqual 通过相等测试检查对象值,而 AreNotSame 检查它们是否不是同一个确切的对象。
很明显,为什么我们想要测试 AreNotEqual(我们关心正在测试的值); 不一样呢? 当您传递引用并希望确保在改组完成后两个引用仍然是同一个对象时,就会发现此功能在测试中的用处。
在现实世界中,我们使用大量缓存对象来减少数据库的往返次数。 将对象移交给缓存系统后,我们的单元测试确保在某些情况下我们返回相同的对象(缓存有效),而在其他情况下我们返回一个新鲜对象(缓存已作废)。 请注意,在这种情况下,AreNotEqual 不一定足够。 如果对象在数据库中具有新的时间戳,但数据还没有“足够不同”而无法通过相等性测试,则 AreNotEqual 不会注意到我们刷新了对象 。
Two things can be equal, but different objects. AreNotEqual checks the objects values via the equality test, while AreNotSame checks that they are not the same exact object.
It is obvious why we would want to test that things AreNotEqual (we care about the values being tested); what about AreNotSame? The usefulness of this in testing is found when you have passed references around and want to make sure that after your shuffling is done that two references are still the same object.
In a real world case, we use a lot of caching objects to mitigate round trips to the database. After an object has been handed off to the cache system, our unit tests ensure that in some cases we get back the same object (cache was valid) and in other cases we get back a fresh object (cache was invalidated). Note that AreNotEqual would not necessary suffice in this case. If the object had a new timestamp in the database, yet the data was not "different enough" to fail an equality test, AreNotEqual wouldn't notice that we refreshed the object.
AreNotSame 进行参考比较,而 AreNotEqual 则相等比较。
AreNotSame does reference comparison, whereas AreNotEqual does an equality comparison.
Assert.AreNotEqual 断言两个值彼此不相等。
Assert.AreNotSame 断言两个变量不指向同一个对象。
示例 1:
示例 2:
Assert.AreNotEqual asserts that two values are not equal to each other.
Assert.AreNotSame asserts that two variables do not point to the same object.
Example 1:
Example 2:
AreNotSame 使用引用相等 (
object.ReferenceEquals
) - 即它们是否是对象的相同实际实例; AreNotEqual 使用概念相等 (.Equals
) - 即它们被认为相等。AreNotSame uses reference equality (
object.ReferenceEquals
) - i.e. are they the same actual instance of an object; AreNotEqual uses conceptual equality (.Equals
) - i.e. are they considered equal.难道 AreNotEqual 检查两个对象在 Equals() 方法中不相等的情况,而 AreNotSame 检查两个对象引用不同的情况。 因此,如果 x 和 y 是两个在 Equals() 方面相等但已单独分配的对象,则 AreNotEqual() 将触发失败的断言,而另一个则不会。
Isn't it so that AreNotEqual checks for the case where two objects are not equal in terms of Equals() method, whereas AreNotSame checks for the case where the two object references are not the same. So if x and y are two objects which are equal in terms of Equals() but have been separately allocated, AreNotEqual() would trigger failing assertion but the other not.