C# Assert.AreNotEqual 和 Assert.AreNotSame 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/543263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:33:47  来源:igfitidea点击:

What's the difference between Assert.AreNotEqual and Assert.AreNotSame?

c#.netunit-testingtestingassert

提问by Dan Esparza

In C#, what's the difference between

在 C# 中,有什么区别

Assert.AreNotEqual

and

Assert.AreNotSame

采纳答案by Jon Skeet

Almost all the answers given here are correct, but it's probably worth giving an example:

这里给出的几乎所有答案都是正确的,但可能值得举个例子:

public static string GetSecondWord(string text)
{
    // Yes, an appalling implementation...
    return text.Split(' ')[1];
}

string expected = "world";
string actual = GetSecondWord("hello world");

// Good: the two strings should be *equal* as they have the same contents
Assert.AreEqual(expected, actual);

// Bad: the two string *references* won't be the same
Assert.AreSame(expected, actual);

AreNotEqualand AreNotSameare just inversions of AreEqualand AreSameof course.

AreNotEqual并且AreNotSame是刚刚倒AreEqualAreSame课程。

EDIT: A rebuttal to the currently accepted answer...

编辑:对当前接受的答案的反驳......

If you use Assert.AreSamewith value types, they are boxed. In other words, it's equivalent to doing:

如果您使用Assert.AreSamewith 值类型,它们将被装箱。换句话说,它相当于做:

int firstNumber = 1;
int secondNumber = 1;
object boxedFirstNumber = firstNumber;
object boxedSecondNumber = secondNumber;

// There are overloads for AreEqual for various value types
// (assuming NUnit here)
Assert.AreEqual(firstNumber, secondNumber);

// ... but not for AreSame, as it's not intended for use with value types
Assert.AreSame(boxedFirstNumber, boxedSecondNumber);

Neither firstNumbernor secondNumberhas an object value, because intis a value type. The reason the AreSamecall 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.)

既没有firstNumber也没有secondNumber对象值,因为int是值类型。AreSame调用失败的原因是因为在 .NET 中,每次装箱一个值都会创建一个新框。(在 Java 中它有时没有 - 这让我之前感到很困惑。)

Basically you should neveruse AreSamewhen comparing value types. When you're comparing referencetypes, use AreSameif you want to check for identical references; use AreEqualto check for equivalence under Equals. EDIT: Note that there aresituations where NUnit doesn't just use Equalsdirectly; it has built-in support for collections, where the elements in the collections are tested for equality.

基本上你应该永远使用AreSame比较值类型时。比较引用类型时,AreSame如果要检查相同的引用,请使用;用于AreEqual检查 下的等效性Equals。编辑:请注意,在某些情况下,NUnit 不仅直接使用Equals;它内置了对集合的支持,其中集合中的元素被测试是否相等。

The claim in the answer that:

答案中的主张是:

Using the example above changing the int to string, AreSame and AreEqual will return the same value.

使用上面的示例将 int 更改为 string,AreSame 和 AreEqual 将返回相同的值。

entirely depends on how the variables are initialized. If they use string literals, then yet, interning will take care of that. If, however, you use:

完全取决于变量的初始化方式。如果他们使用字符串文字,那么实习生会解决这个问题。但是,如果您使用:

string firstString = 1.ToString();
string secondString = 1.ToString();

then AreSame and AreEqual will almost certainly notreturn the same value.

那么 AreSame 和 AreEqual 几乎肯定不会返回相同的值。

As for:

至于:

The general rule of thumb is to use AreEqual on value types and AreSame on reference types.

一般的经验法则是在值类型上使用 AreEqual,在引用类型上使用 AreSame。

I almost neverwant to check for reference identity. It's rarely useful to me. I want to check for equivalencewhich is what AreEqualchecks for. (I'm not saying that AreSameshouldn't be there - it's a useful method, just far more rarely than AreEqual.)

我几乎不想检查参考身份。对我来说很少有用。我想检查等价这正是AreEqual检查。(我并不是说这AreSame不应该存在 - 这是一种有用的方法,只是比AreEqual.)

回答by Godeke

Two things can be equal, but different objects. AreNotEqual checks the objects valuesvia the equality test, while AreNotSame checks that they are not the same exact object.

两件事可以相等,但对象不同。AreNotEqual通过相等测试检查对象,而 AreNotSame 检查它们是否不是完全相同的对象。

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.

很明显为什么我们要测试 AreNotEqual 的东西(我们关心被测试的值);AreNotSame 呢?当您传递引用并希望确保在完成改组后两个引用仍然是同一个对象时,就会发现这在测试中的用处。

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 freshobject (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 datawas not "different enough" to fail an equality test, AreNotEqual wouldn't notice that we refreshed the object.

在现实世界中,我们使用大量缓存对象来减少到数据库的往返。在一个对象被移交给缓存系统之后,我们的单元测试确保在某些情况下我们得到相同的对象(缓存有效),而在其他情况下我们得到一个对象(缓存无效)。请注意,在这种情况下,AreNotEqual 是不够的。如果对象在数据库中有一个新的时间戳,但数据“不同”到无法通过相等性测试,AreNotEqual 不会注意到我们刷新了对象

回答by ermau

AreNotSamedoes reference comparison, whereas AreNotEqualdoes an equality comparison.

AreNotSame 进行引用比较,而AreNotEqual进行相等比较。

回答by Marc Gravell

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 consideredequal.

AreNotSame 使用引用相等 ( object.ReferenceEquals) - 即它们是否是对象的相同实际实例;AreNotEqual 使用概念上的相等 ( .Equals) - 即它们是否被视为相等。

回答by Antti Huima

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.

难道不是这样 AreNotEqual 检查两个对象在 Equals() 方法方面不相等的情况,而 AreNotSame 检查两个对象引用不相同的情况。因此,如果 x 和 y 是两个在 Equals() 方面相等但已单独分配的对象,则 AreNotEqual() 将触发失败的断言,而另一个则不会。

回答by Ole Lynge

Assert.AreNotEqual asserts that two values are not equal to each other.

Assert.AreNotEqual 断言两个值彼此不相等。

Assert.AreNotSame asserts that two variables do not point to the same object.

Assert.AreNotSame 断言两个变量不指向同一个对象。

Example 1:

示例 1:

int i = 1;
int j = i;
// The values are equal:
Assert.AreEqual(i, j);
// Two value types do *not* represent the same object:
Assert.AreNotSame(i, j);

Example 2:

示例 2:

string s = "A";
string t = s;
// The values are equal:
Assert.AreEqual(s, t);
// Reference types *can* point to the same object:
Assert.AreSame(s, t);