How to check a list is ordered using Fluent Assertions

。_饼干妹妹 提交于 2019-12-23 14:21:48

问题


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?


回答1:


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);



回答2:


It is possible to pass the options like:

listFromApi.Should().BeEquivalentTo(sortedList, opt => opt.WithStrictOrdering());


来源:https://stackoverflow.com/questions/33143875/how-to-check-a-list-is-ordered-using-fluent-assertions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!