How do I exclude a property of all items in IEnumerable when using ShouldBeEquivalentTo?

独自空忆成欢 提交于 2019-12-01 18:27:37

问题


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));

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?


回答1:


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"));


来源:https://stackoverflow.com/questions/15361521/how-do-i-exclude-a-property-of-all-items-in-ienumerable-when-using-shouldbeequiv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!