使用ShouldBeEquivalentTo时,如何排除IEnumerable中所有项的属性?

koj*_*ojo 5 c# unit-testing fluent-assertions

在我的NUnit/FluentAssertions测试中,我使用以下代码将从系统返回的复杂对象与引用对象进行比较:

    response.ShouldBeEquivalentTo(reference, o => o.Excluding(x => x.OrderStatus)
                                               .Excluding(x => x.Id)
                                               .Excluding(x => x.Items[0].Name)
                                               .Excluding(x => x.Items[0].Article)
                                               .Excluding(x => x.ResponseStatus));
Run Code Online (Sandbox Code Playgroud)

但是,这并不是我的意图.我想排除Name,并Article每一个在对象Items列表,不仅为第0.我该如何实现这种情况?

我查看了文档,但没有找到解决方案.我错过了什么吗?

Den*_*men 8

Excluding()的重载提供了一个ISubjectInfo,您可以将其用于更高级的选择条件.有了这个重载,你可以做以下事情:

subject.ShouldBeEquivalentTo(expected, config =>
                config.Excluding(ctx => ctx.PropertyPath == "Level.Level.Text"));
Run Code Online (Sandbox Code Playgroud)