I am using VS 2010, and running my unit tests with NUnit. The following line correctly detects if two lists differ:
CollectionAssert.AreEqual(expected, actual);
However, I would like a better error message than the following:
Expected and actual are both <System.Collections.Generic.List`1[MyNamespace.MyClass]> with 2 elements
Values differ at index [0]
Expected: <MyNamespace.MyClass>
But was: <MyNamespace.MyClass>
In MyNamespace.MyClass, I have implemented the following method:
public new string ToString()
I would expect NUnit to output the following:
Expected and actual are both <System.Collections.Generic.List`1[MyNamespace.MyClass]> with 2 elements
Values differ at index [0]
Expected: <24 ounces of cold beer>
But was: <2.4 ounces of rotten tomatoes>
However, NUnit does not invoke it. What am I missing?
You’ve hidden the method from
objectinstead of overriding it. Use this:Basically NUnit is just calling
object.ToString()– which you hadn’t overridden. Unless it specifically looked for a new method with reflection, it wouldn’t find your one – and overriding is the idiomatic way to do this. Was this just a simple mistake, or did you mean to shadow the method for some reason?