2011-11-18 40 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")); 

希望别人会发现这个问题/答案在将来有用。