本文介绍了Assert.AreNotEqual 和 Assert.AreNotSame 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,

Assert.AreNotEqual

Assert.AreNotSame

推荐答案

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

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);

AreNotEqualAreNotSame 当然只是 AreEqualAreSame 的反转.

AreNotEqual and AreNotSame are just inversions of AreEqual and AreSame of course.

目前的反驳接受的答案...

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

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

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);

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

Neither firstNumber nor secondNumber has an object value, because int is a value type. The reason the AreSame 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.)

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

Basically you should never use AreSame when comparing value types. When you're comparing reference types, use AreSame if you want to check for identical references; use AreEqual to check for equivalence under Equals. Note that there are situations where NUnit doesn't just use Equals directly; it has built-in support for collections, where the elements in the collections are tested for equality.

答案中的声明:

使用上面的示例更改int 到字符串,AreSame 和 AreEqual将返回相同的值.

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

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:

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

然后 AreSameAreEqual 几乎肯定不会返回相同的值.

then AreSame and AreEqual will almost certainly not return the same value.

至于:

一般的经验法则是使用AreEqual 上的值类型和 AreSame 上引用类型.

我几乎从不想检查参考身份.对我来说很少有用.我想检查 equivalence 这就是 AreEqual 检查的内容.(我并不是说 AreSame 不应该存在 - 这是一种有用的方法,只是比 AreEqual 少得多.)

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 that AreSame shouldn't be there - it's a useful method, just far more rarely than AreEqual.)

这篇关于Assert.AreNotEqual 和 Assert.AreNotSame 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 00:58