简体   繁体   English

如何检查列表是使用Fluent断言进行排序的

[英]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. 我正在使用specflow编写一些单元测试,需要一种方法来检查对象列表是否按特定属性排序。 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? 有没有一种很好的方法可以使用Fluent Assertions完成?

Yes. 是。 You can use BeInAscendingOrder with a lambda. 您可以将BeInAscendingOrder与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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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