John Lam 1997 — 2026

NUnit V2.2 bug with nullable types

I’m spending some time this evening with nullable types, and I discovered that NUnit V2.2 doesn’t handle nullable types correctly. Here’s a test case that fails:

[Test, ExpectedException(typeof(AssertionException))]
public void NUnitAssertEqualBugWithNullableTypes() {
  int? x = null;
  Assert.AreEqual(null, x);
}
3 archived reader comments

Ben Lowery (2004-11-14)

Hi John,

I’m not sure I understand the test case. Is there a specific reason you’re expecting AssertionException rather than just something like Assert.IsFalse(x.Equals(null)) ?

There seem to be two schools of thought when it comes to comparing null to null. It looks like you’re expecting null comparisons to come out false, which is what I would expect as well.

Hmmm… Time to install the CTP and see where this is failing. Tracing through what I would expect in disassembly isn’t revealing anything obvious. :)

John Lam (2004-11-14)

I haven’t looked at how NUnit implements Assert.AreEqual(). However, if you compare using the == operator in C#, the comparison against null works as you would expect.

Charlie Poole (2004-11-19)

Our comparison code returns true if both arguments are null, and false if only one is null. There is special code for floats, doubles, arrays and strings. Everything else is handled using return expected.Equals( actual );

But if anyone has ideas that would allow our current code (VS2003/.NET 1.1) to work correctly for nullable types when run under .NET 2.0, please let me know.