Search code examples
c#unit-testingfluent-assertions

How can I assert a collection is sorted by 2 properties with FluentAssertions?


I recently discovered that FluentAssertions has a collection assertion named BeInAscendingOrder. Awesome!

public class MyItems
{
    public int SequenceNumber { get; set; }
    public int Name { get; set; }
}

IList<int> resultingList = myClassUnderTest.GetOrderedList();

resultingList.Should().BeInAscendingOrder(m => m.SequenceNumber);

But now I would like to test that a list is sorted by 2 properties. Is this possible?


Solution

  • You can't really. The lambda you pass in there is translated in a property expression, not an executable lambda statement. And there's no overload to provide your own implementation of IComparer.

    Your best bet is to generate a collection that contains those items in the right order and comparing it with Should().Equal. This will assert both collections contain the same elements in the same order.