-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
Problem
Many tests use count-based assertions like:
Assert.That(menu.Items, Has.Count.EqualTo(8));
Assert.That(results, Has.Count.EqualTo(3));When these fail, the error message provides no diagnostic value:
Expected: 8
But was: 7
You have no idea what's missing or what changed.
Proposed Solution
Replace count assertions with explicit expected value comparisons:
// Before (poor diagnostics):
Assert.That(menu.Items, Has.Count.EqualTo(4));
// After (clear diagnostics):
var expected = new[] { "New", "Open", "Save", "Exit" };
Assert.That(menu.Items.Select(i => i.Text), Is.EqualTo(expected));On failure, NUnit will show exactly which items differ:
Expected: ["New", "Open", "Save", "Exit"]
But was: ["New", "Open", "Exit"]
Missing: ["Save"]
Examples Found
Rdmp.UI.Tests/ArbitraryFolderNodeTests.cs- menu item countsRdmp.UI.Tests/AggregateEditorUITests.cs- dropdown item countsRdmp.UI.Tests/HistoryProviderTests.cs- history counts- Various tests using
Has.Count.EqualTo(N)pattern
Guidelines
- Order matters: Use
Is.EqualTo(expected)when order is significant - Order doesn't matter: Use
Is.EquivalentTo(expected)for set comparisons - Subset checks: Use
Is.SupersetOf(expected)when only verifying certain items exist - Named values: Extract expected values to named constants/arrays for clarity
Exceptions
Some count assertions are appropriate:
- Row counts from database queries where content varies
- Verifying "at least N" with
Is.GreaterThanOrEqualTo - Performance tests measuring quantities
The goal is improving tests where the specific items are known and stable.