Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a list is ordered using Fluent Assertions

I am writing some unit tests using specflow and need a way to check whether a list of objects is ordered by a specific property. Currently I am doing it like this, but I am not sure if this is the best way to go about it.

var listFromApi = listOfObjects;

var sortedList = listFromApi.OrderBy(x => x.Property);

Assert.IsTrue(listFromApi.SequenceEqual(sortedList));

Is there a nice way this can be done using Fluent Assertions?

like image 986
TomJerrum Avatar asked Oct 15 '15 08:10

TomJerrum


2 Answers

Yes. You can use BeInAscendingOrder with a lambda.

listFromApi.Should().BeInAscendingOrder(x => x.Property);

For extra clarity at the expense of performance, you can also assert on content equivalence:

listFromApi.Should().BeEquivalentTo(listOfObjects)
    .And.BeInAscendingOrder(x => x.Property);
like image 178
Paul Hicks Avatar answered Nov 09 '22 00:11

Paul Hicks


It is possible to pass the options like:

listFromApi.Should().BeEquivalentTo(sortedList, opt => opt.WithStrictOrdering());
like image 6
Guilherme Porto Avatar answered Nov 08 '22 22:11

Guilherme Porto