Here is a nice trick to apply when using CollectionAssert.AreEquivalent of MSTest. While the signature of the method is:
(ICollection expected, ICollection actual)
When using them in your code switch between the actual and expected. Pass your execution result as the first argument, and you expected collection as the second. Doing this will give a better more informative error message in during failures.
Lets see an example,
I have a test which ends with this:
var actual = CrapAnalyzer.CreateCrapReport("", "");
var expected = new Dictionary<string, double>();
expected["MyClass.Method1"] = 1.34;
expected["MyClass.Method2"] = 1.22;
expected["MyClass.Method3"] = 5;
CollectionAssert.AreEquivalent
( expected as ICollection,
actual as ICollection );
When run as is I get the following:
CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of <[MyClass.Method3, 5]>. The actual collection contains 0 occurrence(s).
when I switch between the collections i get:
CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of <[MyClass.Method3, 1.12]>. The actual collection contains 0 occurrence(s).
While at first glance both see the same, the first one gives me no added information that is not written in the test code. The second error on the other hand, actually gives a good description on what happened. Reading it I can clearly see that the result value for Method3 was 1.12 instead of 5 expected by the test code. Now I know the actual difference and there’s a good chance that I can understand what went wrong without opening a debugger.
0 comments:
Post a Comment