Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert.ReferenceEquals() Passes where Object.ReferenceEquals() returns 'false' in Visual Studio Test

Tags:

In attempting to create an initial, failing unit test in Visual Studio Professonal 2008's test capabilities, I can't seem to get Assert.ReferenceEquals() to correctly fail when an object instance is not equal to a null reference. Note that object.ReferenceEquals() is correctly returning false for this same comparison.

Here is my class code:

public static class Project {     public static object TheObject { get; set; }      public static void Startup(object theObject)     {         // ToDo: Project.Startup(): Test.         // ToDo: Project.Startup(): Implement.     } } 

And then here are the key aspects of my test class:

[TestClass()] public class ProjectTest {     [TestMethod()]     public void StartupTest()     {         object obj = "hello";         Project.Startup(obj);             Assert.ReferenceEquals(obj, Project.TheObject); // Test Passes!?!     } } 

Note that the static void Startup(object) method is empty, so the static object TheObject property is never set and remains null. So, clearly, Assert.ReferenceEquals(obj, Project.TheObject) should fail, but somehow this test passes.

Note that changing

Assert.ReferenceEquals(obj, Project.TheObject)

to

Assert.IsTrue(object.ReferenceEquals(obj, Project.TheObject))

causes this test to correctly fail.

This seems too simple, and yet I cannot see what's going wrong here. If someone can point out the error in my ways, I would be much obliged.

Thanks in advance,

Mike

Update Answered by James Avery:

Ah, an how silly I feel now. I knew it had to be something like this. Wow.

Sure enough, 'GoToDefinition' takes me to 'Object.ReferenceEquals()'. So typing "Assert.ReferenceEquals()" is really System.Object.ReferenceEquals(), which in my case was quietly returning 'false'. This, of course, has nothing to do with actually failing an assertion, so the test passes. Amazing.

Thanks James.

like image 709
Mike Rosenblum Avatar asked Apr 19 '09 22:04

Mike Rosenblum


People also ask

How does assert Areequal work?

Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal. 42L is not equal to 42.

How to use Assert Equals in c#?

Assert. Equal(expected.Name, actual.Name); The first example fails due to the way comparison works for reference types. By default, the equality operation for those types will only assert whether the two objects being compared are the same, namely your variables are pointing to the same object within the memory heap.

What does ReferenceEquals stand for?

ReferenceEquals() Method is used to determine whether the specified Object instances are the same instance or not. This method cannot be overridden.

What is the most accurate way to check for equality by reference?

To check for reference equality, use ReferenceEquals. To check for value equality, use Equals or Equals. By default, the operator == tests for reference equality by determining if two references indicate the same object, so reference types do not need to implement operator == in order to gain this functionality.


1 Answers

The ReferenceEquals method you are calling is the static method available on all reference objects, it is not part of the testing framework. If you look it is returning a boolean value whereas a normal assertion would be void. This is definitely confusing, .AreSame() is the assertion you are looking for.

like image 80
James Avery Avatar answered Oct 19 '22 10:10

James Avery