A few days ago my colleague asked me for help with a misty error occurring in tests. The tests were comparing collections using the CollectionAssert class from the NUnit framework. After a short investigation, I proposed a simple solution that helped in determining the source of the problem.
Let’s start with defining the problem. The test below is as simple as possible. It checks two collections using CollectionAssert tool.
The collections are different, so the error message occurs:
As we can see, the message is very misty. Imagine a complex class with many compared properties – determining what’s incorrect there may be troublesome.
But, there is a small improvement that we can make to get a better vision. Once again, check this test code:
This time we extended SimpleClass with an overridden ToString() method. It’s enough to get improved error messages:
Notice that I also decorated VerboseClass with the DebuggerDisplay attribute. It doesn’t help with the message produced by CollectionAssert, but may help with debugging routines. If you wish, you can also change this attribute to display the message produced by the ToString method:
Then, during debugging, you can easily see the internals of the list items:
The two tips served today are small but can, hopefully, spare some time that you can use in a better manner 🙂