简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM