2013-03-12 23 views
5

在我的NUnit/FluentAssertions测试中,我比较从我的系统,使用下面的代码引用一个返回的复杂对象:如何在使用ShouldBeEquivalentTo时排除IEnumerable中所有项目的属性?

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

然而,这不正是我的本意。我想排除NameArticle对象在Items列表中,而不仅仅是0。我如何实现这种情况?

我查看了documentation,并没有找到解决方案。我错过了什么吗?

回答

7

Excluding()的重载提供了一个ISubjectInfo,您可以使用它来获取更高级的选择条件。有了这样的过载,你可以做这样的事情:

subject.ShouldBeEquivalentTo(expected, config => 
       config.Excluding(ctx => ctx.PropertyPath == "Level.Level.Text")); 
+0

究竟应该如何在我的例子中使用PropertyPath?我已经尝试PropertyPath ==“Items.Name”和“Order.Items.Name”,但它不起作用。 – kojo 2013-03-14 11:35:00

+1

ctx.PropertyPath.EndsWith(“]。Name”)|| ctx.PropertyPath.EndsWith( “]。第二条”)? – 2013-03-15 05:51:49

+0

工作,谢谢。我想,如果我想匹配集合名称,我应该使用正则表达式。 PropertyPath在各种情况下究竟有多精确? – kojo 2013-03-15 11:29:22

相关问题