2011-11-18 39 views
2

我在.NET 3.5 C#應用程序中使用NUnit 2.5.6.10205。我使用NUnit的Collection Constraint來斷言IEnumerable是否由參數排序。NUnit CollectionConstraints例外

它似乎並沒有爲我工作,因爲我收到一個異常,指出我的實際值不是IEnumreable。 allEntities是一個List<T>,它實現了IEnumerable<T>。我相信NUnit正在尋找IEnumerable,而不是IEnumerable<T>,但IEnumerable<T>執行IEnumerable。這是一個共同/反對差異的問題嗎?

Assert.That(allEntities, Is.All.Ordered.By("CreationDate")); 

System.ArgumentException : The actual value must be an IEnumerable 
Parameter name: actual 

另外,有沒有什麼方法可以表達使用Lambda的排序屬性?對屬性使用文字字符串會使其變脆。

回答

1

沒有必要爲All,請嘗試:

Assert.That(allEntities, Is.Ordered.By("CreationDate")); 
+0

我會給你支票所以它看起來並不像我提出了一個問題,只是回答我自己:) –

+0

THX,但你在15秒內速度更快:)。 +1給你更完整的答案。 – nemesv

2

我所用的所有contstraint,但是這是用來做列表中的每個項目的斷言,即

// checks that for each T in myList, that it is greater than 5 
Assert.That(myList, Is.All.GreaterThan(5)); 

NUnit:「適用約束到集合中的每個項目,隨後僅在他們都成功了。「

我想測試列表本身的屬性,所以我想:

// checks that the list itself is ordered by the property CreationDate 
Assert.That(allEntities, Is.Ordered.By("CreationDate")); 

希望別人會發現這個問題/答案在將來有用。