You have reached the blog of Keith Elder. Thank you for visiting! Feel free to click the twitter icon to the right and follow me on twitter.

When Bad UnitTests Don’t Know They Are Good

Posted by Keith Elder | Posted in .Net | Posted on 10-04-2008

I was writing a test earlier today  trying to play nice with the red first green later crowd.  When I first wrote my test it showed red.  It didn’t work, this was good.  I went in and wrote what I thought was “green” code.  I ran it and whoops I didn’t mark my dummy class public so the XML serializer blew up.  My test failed again.  I fixed that quicly and ran the test again and it failed again!  I looked at the message and this was staring me in the face.

Assert.AreEqual failed.
Expected: <system.xml.serialization.xmlserializer>
Actual:       <system.xml.serialization.xmlserializer>

Yah.  I’m at a loss too.  Doesn’t this test know that it was suppose to pass?  I feel like Adam Sandler in Happy Gilmore.    “Why won’t you just get in the hole?!!!”. 

Comments (3)


The problem is that the test is comparing two reference type objects, which will never be the same. You either need to use Assert.IsTrue(obj1.Equals(obj2)), or compare parts of the objects, like obj1.prop and obj2.prop.

And hopefully I didn’t get the reference type/value type thing backwards 🙂

Hope this helps

Thanks Justin,

I was using Assert.AreEqual. I got to refactoring some stuff after that anyway so I’ll come back to it later.


I came across this type of error before, it might be the same as what you’re getting. My problem was my Assert statement. I used “Microsoft.VisualStudio.TestingTools.UnitTesting.Assert.AreSame”

The docs say: “AreSame(object, object) – Verifies that two specified object variables refer to the same object. The assertion fails if they refer to different objects”

So, if you have two objects of the same type but do not use the same memory you could get that error.

In my case, I changed “AreSame” to “AreEqual” and it went away.

My best advise is make sure your assert is checking exactly what you think it’s checking.

Just a thought, but I hope it helps.

Write a comment