0

I'm trying to integrate ScalaCheck into our unit specifications in specs2. I must be missing something on how this works:

class TestCase extends PlaySpecification with ScalaCheck {
    "The container data model" should {
        val validIdRange = Gen.choose(1,GPDataTypes.integer._2)

        def chk(n: Int, d: Int) = {
            val g = new GPGlimple(n, true, true)
            g.accountId mustEqual 1 // should fail (actually equals n)
            g.id must beNone
            g.created must beNone
        }

        val idPairs = forAll(validIdRange, validIdRange) { (n: Int, d: Int) => chk(n, d) }
        "support basic glimple constructors" in {
            idPairs.check
        }
    }
}

However, this test executes without failure. It should definitely fail on the 'mustEqual 1' (which I've intentionally changed to 1, it actually equals n). The results in the test output, however...

[info] The glimple container data model should [info] + support basic glimple constructors

Zaphod
  • 1,387
  • 2
  • 17
  • 33
  • Can you try to remove `.check`? You should need that. There is an implicit `AsResult` instance for a ScalaCheck `Prop` and it should take care of throwing exception when there is a failure. – Eric May 15 '15 at 08:29
  • Thanks – a good point, and yes, it seems that .check has been deprecated in favor of newer APIs (unfortunately, not all the examples/books/docs are quite in sync... but we're figuring it out). – Zaphod May 15 '15 at 21:01

1 Answers1

0

Ok, we figured it out after a few pointers regarding the deprecation of .check and how ScalaCheck and specs2 work together.

This is where I ended up:

class TestGlimpleModel extends PlaySpecification with ScalaCheck {
    "The glimple container data model" should {
        val validIdRange = Gen.choose(1, GPDataTypes.integer._2)
        val invalidIdRange = Gen.choose(GPDataTypes.integer._1, 0)
        val booleanRange = Gen.oneOf(true, false)

        "construct with valid id, created, and account parameters" in {
            forAll(validIdRange, validIdRange, booleanRange, booleanRange) { (n: Int, m: Int, b: Boolean, c: Boolean) =>
                ( Try { new GPGlimple(Option(GPID(n)), Option(new DateTime), GPID(m), b, c) } must beSuccessfulTry )
            }
        }

        "throw exceptions if the id or account parameters are invalid" in {
            forAll(invalidIdRange, invalidIdRange, booleanRange, booleanRange) { (n: Int, m: Int, b: Boolean, c: Boolean) =>
                 ( Try { new GPGlimple(Option(GPID(n)), Option(new DateTime), GPID(m), b, c) } must beFailedTry )
            }
        }
    }
}

Maybe not the most elegant, but I'm just getting started with ScalaCheck. This seems to be much closer to the intended API.

I created several generators that create random sets of valid or invalid IDs, and also a randomized set of booleans. Then the two test cases attempt to create our object from the data, in a Try{} and check for success or failure (as appropriate).

The one thing I'm still working on is figuring out how to generate randomized soda-time DateTime instances. Haven't been able to get the syntax quite right on that yet...

Zaphod
  • 1,387
  • 2
  • 17
  • 33