Back to Xunit

xUnit2031

site/xunit.analyzers/rules/xUnit2031.md

latest838 B
Original Source

Cause

A violation of this rule occurs when using a LINQ Where clause to filter items before calling Assert.Single.

Reason for rule

A more concise overload of Assert.Single allows filtering and shows intent better.

How to fix violations

To fix a violation of this rule, use the overload of Assert.Single that takes a filter function.

Examples

Violates

csharp
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));
    }
}

Does not violate

csharp
using Xunit;

public class xUnit2031
{
    [Fact]
    public void TestMethod()
    {
        int[] collection = [1, 3, 5, 6, 9];

        Assert.Single(collection, i => i % 2 == 0);
    }
}