I prefer:
Assert.That(collection, Has.Count.EqualTo(expectedCount));
Simply because .Count
and Count()
could be incorrectly overridden (which should be tested in a different unit test if they are, although I don't know how nunit is actually doing the count internally). Bascially I don't want my actual to have any side affect logic. Calling Count
or Count()
should probably be done in the act like:
// Arrange
var expectedCount = 8;
// Act
var list = GetList();
var actualCount = list.Count();
// Assert
Assert.That(actualCount, Is.EqualTo(expectedCount));
Both read fairly the same, but the first one has less logic to look over when it fails.
Update: dec-1-2020
Considering the number of upvotes this has received, I think I should mention that using https://fluentassertions.com/ (FA) is advantageous. This could be written in the following ways and reads much nicer:
// Arrange
// Act
var list = GetList();
// Assert
list.Should().BeEmpty();
or
// Arrange
// Act
var list = GetList();
// Assert
list.Should().HaveCount(8);
or
// Arrange
// Act
var list = GetList();
// Assert
list.Should().HaveCountLessThanOrEqualTo(10);
Beyond the OP question, FA can do some very advanced collection rules:
// Arrange
// Act
var persons = GetPersons();
// Assert
persons.Should().BeInAscendingOrder(p => p.LastName)
.And().OnlyHaveUniqueItems(p => p.id);
The framework goes way beyond collections and adds readability to testing other topics as well.
// Arrange
// Act
Action action = () => 8 / 0;
//
action.Should().Throw<DivideByZeroException>();
I have no association with fluent assertions.