I have in my test suite a test that goes something like this:
[Fact]
public void VerifySomeStuff()
{
var stuffCollection = GetSomeStuff();
Assert.Equal(1, stuffCollection.Count());
}
This test works as I expect, but when I run it xUnit prints a warning:
warning xUnit2013: Do not use Assert.Equal() to check for collection size.
However, no alternative is suggested in the warning, and a google search takes me to the source code in xUnit for the test that verifies this warning is printed.
If Assert.Equal()
isn't the correct way to verify the lenght of a collection, what is?
To clarify: I realize that I could "trick" xUnit into not emitting this warning by e.g. extracting a variable or using Assert.True(stuff.Count() == 1)
instead. The latter is just hacky, and the former feels like if xUnit is e.g. trying to avoid multiple iterations of an IEnumerable<T>
, then this is the wrong way to go (because I'll get compiler hints about that separately, if it's an issue), and xUnit itself should never have to evaluate the input more than once (in fact it probably will get the same input regardless of variable extraction, because of how C# function calling works).
So, I'm not just interested in removing that warning from my output. An answer to my question also explains why that warning is included in the library in the first place, and why whatever approach I should use instead is better.