site/xunit.analyzers/rules/xUnit2023.md
A violation of this rule occurs when you use Assert.Collection to verify that a collection
has a single item in it.
Using Assert.Collection is designed for inspecting multiple items in a collection. When a
collection is only expected to have a single item in it, using Assert.Single to get the item
makes the code more concise and readable, especially when performing multiple assertions against
the item.
To fix a violation of this rule, replace Assert.Collection with Assert.Single and move the
assertions out into the body of the unit test.
using Xunit;
public class xUnit2023
{
[Fact]
public void TestMethod()
{
var collection = new[] { 1 };
Assert.Collection(
collection,
item => Assert.Equal(1, item)
);
}
}
using Xunit;
public class xUnit2023
{
[Fact]
public void TestMethod()
{
var collection = new[] { 1 };
var item = Assert.Single(collection);
Assert.Equal(1, item);
}
}