Scalatest Generator-driven 属性 检查 Eclipse 中的编译错误。好的,在 SBT 中。

Scalatest Generator-driven property checks compilation error in Eclipse. Ok in SBT.

我正在尝试使用 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中编译报错如下:

type mismatch; found : (org.scalacheck.Gen[A], DataGeneratorTest.this.PropertyCheckConfigParam*) required: ?0C[?0E] Note that implicit conversions are not applicable because they are ambiguous: both method ArrowAssoc in object Predef of type [A](self: A)ArrowAssoc[A] and method Ensuring in object Predef of type [A](self: A)Ensuring[A] are possible conversion functions from (org.scalacheck.Gen[A], DataGeneratorTest.this.PropertyCheckConfigParam*) to ?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...

[success] Total time: 37 s, completed 26-Mar-2015 20:04:15

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

环境:

你能不能post完整测试class?

我 运行 遇到了完全相同的问题,发现与使用 Inspectors 特性以及 PropertyChecks 特性存在冲突。从我的测试 class 中删除 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