使用ShouldBeEquivalentTo时,如何排除IEnumerable中所有项目的属性? [英] How do I exclude a property of all items in IEnumerable when using ShouldBeEquivalentTo?
问题描述
在我的NUnit/FluentAssertions测试中,我使用以下代码将系统返回的复杂对象与参考对象进行了比较:
In my NUnit/FluentAssertions tests I compare the complex object returned from my system with a reference one using the following code:
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));
但是,这与我的意图不完全相同.我想排除Items
列表中的每个对象的Name
和Article
,而不仅限于第0个.如何实现这种情况?
However, this is not exactly what I intended. I'd like to exclude Name
and Article
for every object in Items
list and not only for the 0th. How do I implement this scenario?
我已经浏览了文档,但没有找到解决方案.我想念什么吗?
I've looked through the documentation and din't find the solution. Am I missing something?
推荐答案
Exclusion()的重载提供了ISubjectInfo,您可以将其用于更高级的选择条件.有了这种超载,您可以执行以下操作:
There's an overload of Excluding() that provides an ISubjectInfo that you can use for more advanced selection criteria. With that overload, you can do stuff like:
subject.ShouldBeEquivalentTo(expected, config =>
config.Excluding(ctx => ctx.PropertyPath == "Level.Level.Text"));
这篇关于使用ShouldBeEquivalentTo时,如何排除IEnumerable中所有项目的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!