Short Walks - XUnit Warning

April 14, 2018

As with many of these posts - this is more of a “note to self”.

Say you have an assertion that looks something like this in your Xunit test:



Assert.True(myEnumerable.Any(a => a.MyValue == "1234"));

In later versions (not sure exactly which one this was introduced it), you’ll get the following warning:

warning xUnit2012: Do not use Enumerable.Any() to check if a value exists in a collection.

So, Xunit has a nice little feature where you can use the following syntax instead:



Assert.Contains(myEnumerable, a => a.MyValue == "1234");



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024