Recently I was doing the first cut of refactoring the console program and I noticed that we wrote the following test code quite often:
IResultXmlTransform transform = transforms.Single();
var transform = Assert.IsType<XslStreamTransformer>(...);
As I looked around we wrote this same type of code over and over. What I proposed was to write a new Assert named Assert.Single() which would take an IEnumerable<T> (and reluctantly its non-generic equivalent) and Brad suggested if there was only one element then the Assert would return that element. After introducing this new Assert the above code likes like the following:
IResultXmlTransform transform = Assert.Single(transforms);
Not only is the more explicit it provides a better message. If the previous one had failed you would have received a LINQ exception. When we made the changes to our code we found that it affected about 3.5% of the Asserts in the xUnit.net code. Note: xUnit.net works with .NET Framework 2.0 and higher.
I ran this by Kent Beck and we discussed the idea of having Asserts return useful values. His concern was that it was unexpected which can be a problem because its not discoverable. However, he said (I am paraphrasing) that if that many asserts were changed then the code is telling you something and its good to listen.