1
votes

I am trying to do property-based testing using ScalaTest. I have written a few test cases using 2 custom generators and they work fine. However, the moment I define a forAll with one custom generator, such as below:

  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)
      }
    }
  }

I got compilation error in Eclipse as follows:

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]

I tried the example in ScalaTest documentaiton: http://www.scalatest.org/user_guide/generator_driven_property_checks

by doing

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

and got the same error.

However, when I compile it in SBT, there is no error.

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

I am not sure what is wrong. Can someone help? Thanks.

Environment:

  • OS: Windows 7 Enterprise SP1 64-bit
  • Scala IDE: 4.0.0
  • Scala Version: 2.11.6
  • ScalaTest Version: 2.2.4
  • ScalaCheck Version: 1.12.2
1

1 Answers

4
votes

Can you perhaps post the full test class?

I ran into the exact same issue and found that there was a conflict with using the Inspectors trait along side the PropertyChecks trait. After removing the Inspectors trait from my test class, everything worked fine.

abstract class UnitSpec extends FunSpec
  with Matchers
  with OptionValues
  with Inside
  // with Inspectors     <= this conflicts with PropertyChecks trait
  with PropertyChecks
  with ValidationMatchers
  with MockFactory