Do not use Where clause with Assert.Single
A violation of this rule occurs when using a LINQ Where
clause to filter items before calling Assert.Single
.
A more concise overload of Assert.Single
allows filtering and shows intent better.
To fix a violation of this rule, use the overload of Assert.Single
that takes a filter function.
using System.Linq;
using Xunit;
public class xUnit2031
{
[Fact]
public void TestMethod()
{
int[] collection = [1, 3, 5, 6, 9];
Assert.Single(collection.Where(i => i % 2 == 0));
}
}
using Xunit;
public class xUnit2031
{
[Fact]
public void TestMethod()
{
int[] collection = [1, 3, 5, 6, 9];
Assert.Single(collection, i => i % 2 == 0);
}
}