ScalaCheck Arbitrary case class with random data generation (Magnolia)

236 views Asked by At

Using a basic example I'm attempting to randomly generate a bunch of Person (case class Person(name: String, age: Int) instances using this library for random data generation.

The problem I'm running into is when creating an Arbitrary that has bound limits for the age parameter as shown below.

  val arbPersonUnder18: Arbitrary[Person] = Arbitrary(
    for {
      name <- Gen.alphaStr
      age <- Gen.chooseNum(Int.MinValue, 17)
    } yield Person(name, age)
  )

  "validatePersonForAlcohol" should {
    "ensure people with age less than 18 cannot buy alcohol" in {
      implicit val _: Arbitrary[Person] = arbPersonUnder18
      forAll { person: Person =>
        ...
      }
    }
  }

Which results in could not find implicit value for parameter arbA: org.scalacheck.Arbitrary[pbtexample.Person]

I can't see why it's not able to find the arbitrary it needs, any advice would be great.

1

There are 1 answers

0
jwvh On BEST ANSWER

Even though an implicit value is seldom, if ever, referenced by name, it still needs one, what the language spec calls a "stable identifier."

Using _ as the variable name tells the compiler that it can forget about this value after it's been created.