2

我在 .NET 3.5 C# 应用程序中使用 NUnit 2.5.6.10205。我正在使用 NUnit 的Collection Constraint来断言 IEnumerable 是否按参数排序。

它似乎对我不起作用,因为我收到一个异常,表明我的实际值不是 IEnumreable。allEntities 是一个List<T>实现IEnumerable<T>. 我相信 NUnit 正在寻找一个IEnumerable,而不是一个IEnumerable<T>,而是IEnumerable<T>实现IEnumerable。这是 co / contra 方差的问题吗?

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

.

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

另外,有什么方法可以使用 Lambda 表达排序属性吗?对属性使用文字字符串会使它变得脆弱。

4

2 回答 2

2

我使用的是 All 约束,但这用于对列表中的每个项目进行断言,即

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

希望其他人将来会发现这个问题/答案很有用。

于 2011-11-18T13:51:12.723 回答
1

不需要All,试试:

Assert.That(allEntities, Is.Ordered.By("CreationDate"));
于 2011-11-18T13:51:25.493 回答