Do not use Enumerable.Any() to check if a value exists in a collection
A violation of this rule occurs when Enumerable.Any
is used to check if a value matching a predicate exists in a collection.
There are specialized assertions for checking for elements in collections.
Replace Assert.True
with Assert.Contains
and/or Assert.False
with Assert.DoesNotContain
.
using System.Linq;
using Xunit;
public class xUnit2012
{
[Fact]
public void TestMethod()
{
var result = new[] { "Hello" };
Assert.True(result.Any(value => value.Length == 5));
}
}
using Xunit;
public class xUnit2012
{
[Fact]
public void TestMethod()
{
var result = new[] { "Hello" };
Assert.Contains(result, value => value.Length == 5);
}
}