1

我正在尝试使用 ScalaTest 进行基于属性的测试。我使用 2 个自定义生成器编写了一些测试用例,它们运行良好。但是,当我使用一个自定义生成器定义 forAll 时,如下所示:

  it should "generate a given number of weights whose total is less than the given total when the given number is > 0, with default upperBound, lowerBound and total" in {
    // TODO: Find out why single parameter does not work. maxSize here is a workaround
    forAll(reasonableSizes) { (size: Int) =>
      whenever(size > 0) {
        val range = random.nextLong.abs
        val values = DataGenerator.generatePartialWeights(size)
        values should have length size
        every(values) should (be >= BigDecimal(0) and be <= BigDecimal(1))
        values.sum should be < BigDecimal(1)
      }
    }
  }

我在 Eclipse 中得到如下编译错误:

类型不匹配; 找到: (org.scalacheck.Gen[A], DataGeneratorTest.this.PropertyCheckConfigParam*) 需要:?0C[?0E] 请注意,隐式转换不适用,因为它们不明确:在 [A] 类型的对象 Predef 中的两个方法 ArrowAssoc (self: A)ArrowAssoc[A] 和方法 Ensuring in object Predef of type [A](self: A)Ensuring[A] 是来自 (org.scalacheck.Gen[A], DataGeneratorTest.this.PropertyCheckConfigParam* 的可能转换函数) 到?0C[?0E]

我尝试了 ScalaTest 文档中的示例:http ://www.scalatest.org/user_guide/generator_driven_property_checks

通过做

  it should "testing" in {
    val evenInts = for (n <- Gen.choose(-1000, 1000)) yield 2 * n
    forAll(evenInts) { (n) => n % 2 should equal(0) }
  }

并得到同样的错误。

但是,当我在 SBT 中编译它时,没有错误。

sbt compile Java HotSpot(TM) 64-Bit Server [info] Loading project definition from C:\xxx [info] Set current project to cree (in build file:/C:/xxx) [info] Compiling 20 Scala sources to C :\xxx\target\scala-2.11\classes...

[成功] 总时间:37 s,完成时间 26-Mar-2015 20:04:15

我不确定出了什么问题。有人可以帮忙吗?谢谢。

环境:

  • 操作系统:Windows 7 Enterprise SP1 64 位
  • 斯卡拉 IDE:4.0.0
  • 斯卡拉版本:2.11.6
  • ScalaTest 版本:2.2.4
  • ScalaCheck 版本:1.12.2
4

1 回答 1

4

您能否发布完整的测试课程?

我遇到了完全相同的问题,发现在使用Inspectorstrait 的同时使用 trait 存在冲突PropertyChecks。从我的测试类中删除Inspectors特征后,一切正常。

abstract class UnitSpec extends FunSpec
  with Matchers
  with OptionValues
  with Inside
  // with Inspectors     <= this conflicts with PropertyChecks trait
  with PropertyChecks
  with ValidationMatchers
  with MockFactory
于 2015-04-22T05:03:46.647 回答