【问题标题】:NUnit CollectionConstraints exceptionsNUnit 集合约束异常
【发布时间】:2011-11-18 13:42:20
【问题描述】:

我在 .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 来表达排序属性?对属性使用文字字符串会使它变得脆弱。

【问题讨论】:

    标签: c# nunit nunit-2.5


    【解决方案1】:

    我使用的是 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"));
    

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

    【讨论】:

      【解决方案2】:

      不需要All,试试:

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

      【讨论】:

      • 我会给你支票,所以看起来我提出问题只是为了自己回答:)
      • 谢谢,但你用 15 秒更快:)。为您更完整的答案 +1。
      猜你喜欢
      • 2010-09-07
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多