如何使用Fluent断言检查列表 [英] How to check a list is ordered using Fluent Assertions
本文介绍了如何使用Fluent断言检查列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用specflow编写一些单元测试,并且需要一种方法来检查对象列表是否由特定属性排序.目前,我正在这样做,但是我不确定这是否是最好的解决方法.
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));
是否可以使用Fluent断言来实现此目的?
Is there a nice way this can be done using Fluent Assertions?
推荐答案
是.您可以将BeInAscendingOrder
与lambda一起使用.
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);
这篇关于如何使用Fluent断言检查列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文