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.
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.
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.
ReferenceEquals() Method is used to determine whether the specified Object instances are the same instance or not. This method cannot be overridden.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With