What is the difference between Assert.AreNotEqual and Assert.AreNotSame?

In C #, what's the difference between

Assert.AreNotEqual 

and

 Assert.AreNotSame 
+60
c # assert unit-testing testing
Feb 12 '09 at 21:07
source share
6 answers

Almost all of the answers given here are correct, but you should probably give 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); 

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

EDIT: Denial of the currently accepted answer ...

If you use Assert.AreSame with type values, they are put into the box. In other words, this is 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 not intended for use with value types Assert.AreSame(boxedFirstNumber, boxedSecondNumber); 

Neither firstNumber nor secondNumber have an object value, because int is a value type. The reason the AreSame call fails is because in .NET the box value creates a new box every time. (In Java, sometimes this is not the case - it has already bit me.)

Basically, you should never use AreSame when comparing value types. When you compare link types, use AreSame if you want to check for identical links; use AreEqual to check equivalence in Equals . EDIT: Note that there are situations where NUnit does not just use Equals directly; it has built-in support for collections, where items in collections are checked for equality.

The requirement in response is that:

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

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

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

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

Concerning:

A general rule is to use AreEqual over value types and AreSame on reference types.

I almost never want to verify link identification. This is rarely useful to me. I want to check for equivalence, which AreEqual checks. (I'm not saying that AreSame should not be there - this is a useful method, much less often than AreEqual .)

+78
Feb 12 '09 at 21:21
source share

Two things can be equal, but different objects. AreNotEqual checks the values โ€‹โ€‹of objects through an equality test, while AreNotSame checks that they are not an exact exact object.

Obviously, why we would like to check that AreNotEqual things (we care about the tested values); What about AreNotSame? The usefulness of this in testing is revealed when you have passed links around and want to make sure that after your shuffling is done, that the two links remain the same object.

In the real world, we use many cache objects to reduce rounded database visits. After the object has been transferred to the cache system, our unit tests ensure that in some cases we return the same object (the cache was valid), and in other cases we return a new object (the cache was invalid). Please note that in this case, there are not enough AreNotEqual data. If the object had a new timestamp in the database, but the data was not โ€œdifferent enoughโ€ to not run the equality test, AreNotEqual would not notice that we updated the object.

+25
Feb 12 '09 at 21:10
source share

AreNotSame performs reference comparisons, while AreNotEqual compares equality.

+18
Feb 12 '09 at 21:11
source share

Assert.AreNotEqual claims that the two values โ€‹โ€‹are not equal to each other.

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

Example 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:

 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);
+7
Feb 12 '09 at 21:13
source share

AreNotSame uses reference equality ( object.ReferenceEquals ) - that is, they are the same actual instance of the object; AreNotEqual uses conceptual equality ( .Equals ) - that is, they are considered equal.

+6
Feb 12 '09 at 21:11
source share

Is it not that AreNotEqual checks the case when two objects are not equal in terms of the Equals () method, while AreNotSame checks where two references to objects do not match. Therefore, if x and y are two objects that are equal in terms of Equals (), but were allocated separately, AreNotEqual () will cause an unsuccessful statement, and the other will not.

+3
Feb 12 '09 at 21:13
source share



All Articles