我正试图用FluentAssession检查嵌套集合的顺序,如下所示。
selectedStudent.MarkSheets.Select(x => x.Comments.Should().BeInDescendingOrder(x => x.CommentedTime));
但这似乎是不正确的,因为即使我检查BeInAscendingOrder(x => x.CommentedTime
,断言也是正确的。如何使用FluentAssession正确测试?
您可以使用AllSatisfy
。
record Comment(DateTime CommentedTime);
record MarkSheet(List<Comment> Comments);
record Student(List<MarkSheet> MarkSheets);
var selectedStudent = new Student(new List<MarkSheet>()
{
new MarkSheet(new List<Comment>()
{
new Comment(new DateTime(2022, 01, 01)),
new Comment(new DateTime(2023, 01, 01))
}),
new MarkSheet(new List<Comment>()
{
new Comment(new DateTime(2023, 01, 01)),
new Comment(new DateTime(2024, 01, 01))
})
});
selectedStudent.MarkSheets.Should().AllSatisfy(x =>
x.Comments.Should().BeInDescendingOrder(x => x.CommentedTime));
正常故障
Expected selectedStudent.MarkSheets to contain only items satisfying the inspector:
At index 0:
Expected x.Comments {Comment { CommentedTime = 01/01/2022 00.00.00 }, Comment { CommentedTime = 01/01/2023 00.00.00 }} to be ordered "by CommentedTime" and result in {Comment { CommentedTime = 01/01/2023 00.00.00 }, Comment { CommentedTime = 01/01/2022 00.00.00 }}
At index 1:
Expected x.Comments {Comment { CommentedTime = 01/01/2023 00.00.00 }, Comment { CommentedTime = 01/01/2024 00.00.00 }} to be ordered "by CommentedTime" and result in {Comment { CommentedTime = 01/01/2024 00.00.00 }, Comment { CommentedTime = 01/01/2023 00.00.00 }}