From 7895517dc1b86d5c5996d1c6973745c1127b86e5 Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Thu, 28 Apr 2022 13:10:47 +0100 Subject: [PATCH] Add ContainSingle.Where to collection examples (#1917) This if very useful and not at all obvious from the docs that this is possible. --- docs/_pages/collections.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/_pages/collections.md b/docs/_pages/collections.md index bd4905ca10..ed0df7a81a 100644 --- a/docs/_pages/collections.md +++ b/docs/_pages/collections.md @@ -80,6 +80,10 @@ IEnumerable otherCollection = new[] { 1, 2, 5, 8, 1 }; IEnumerable anotherCollection = new[] { 10, 20, 50, 80, 10 }; collection.Should().IntersectWith(otherCollection); collection.Should().NotIntersectWith(anotherCollection); + +var singleEquivalent = new[] { new { Size = 42 } }; +singleEquivalent.Should().ContainSingle() + .Which.Should().BeEquivalentTo(new { Size = 42 }); ``` Asserting that a collection contains items in a certain order is as easy as using one of the several overloads of `BeInAscendingOrder` or `BeInDescendingOrder`. The default overload will use the default `Comparer` for the specified type, but overloads also exist that take an `IComparer`, a property expression to sort by an object's property, or a lambda expression to avoid the need for `IComparer` implementations.