Bug in Debug.AssertReferenceEquals?

  • Thread starter Thread starter jmagaram
  • Start date Start date
J

jmagaram

Why does the call to Debug.AssertReferenceEquals not raise a UnitTesting
exception but the call to Assert.IsTrue does? This inconsistency
(Assert.ReferenceEquals returns a bool and Assert.IsTrue returns a void and
throws) is dangerous because your unit tests may pass but in actuality they
fail.

class TestClass { }
TestClass a = new TestClass();
TestClass b = new TestClass();
Assert.ReferenceEquals(a, b);
Assert.IsTrue(object.ReferenceEquals(a, b));
 
jmagaram said:
Why does the call to Debug.AssertReferenceEquals not raise a UnitTesting
exception but the call to Assert.IsTrue does?

There's no method called Debug.AssertReferenceEquals(). I assume you mean
Assert.ReferenceEquals(). That method doesn't really exist either, see below.
This inconsistency
(Assert.ReferenceEquals returns a bool and Assert.IsTrue returns a void and
throws) is dangerous because your unit tests may pass but in actuality they
fail.
It's unfortunately an unavoidable way of how C# works.
class TestClass { }
TestClass a = new TestClass();
TestClass b = new TestClass();
Assert.ReferenceEquals(a, b);

The problem here is basically that the language allows calling static
methods through derived classes, which is confusing. This is the same thing
as calling Object.ReferenceEquals(), so it's no wonder it would return a
bool. Viewed this way, every class has a .ReferenceEquals() method.
Assert.IsTrue(object.ReferenceEquals(a, b));
Use Assert.AreSame() instead.
 
Back
Top