2013-03-12 22 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

相關問題