FluentAssertions: equivalence of sorted lists

By default, ShouldBeEquivalentTo() will ignore the order in the collections because in most cases two collections are equivalent if they contain the same items in any order. If you do care about the order, just use one of the overloads of WithStrictOrdering() on the options => parameter.

Example:

var myList = Enumerable.Range(1, 5);
var expected = new[]
{
    1, 2, 3, 4, 5
};

//succeeds
myList.ShouldBeEquivalentTo(expected, options => options.WithStrictOrdering());

//fails
myList.Reverse().ShouldBeEquivalentTo(expected, options => options.WithStrictOrdering());

Read more about these options in the documentation.


Late to the game here but I use the Fluent Assertions version of this here:

actualRows.Should().BeEquivalentTo(expectedRows,options => options.WithStrictOrdering());

It will check all the values of all the properties for equivalence, and with this option, the order matters. If the order does not matter, omit the options param and it will make sure the item from one collection will exist somewhere in the other. Hope this helps someone