4

是否可以在测试 Java 代码的 ScalaTest (其中为 ScalaCheck 属性混合检查器)中创建自定义任意生成器?例如,以下是 forAll 中每个测试所需的步骤

val fund = new Fund()
val fundAccount = new Account(Account.RETIREMENT)
val consumer = new Consumer("John")
.createAccount(fundAccount)
fund.addConsumer(consumer)
fundAccount.deposit(amount)

以上是断言结果等之前的准备代码。

4

1 回答 1

14

你当然可以。这应该让你开始。

import org.scalacheck._
import Arbitrary._
import Prop._

case class Consumer(name:String)

object ConsumerChecks extends Properties("Consumer") {
  lazy val genConsumer:Gen[Consumer] = for {
    name <- arbitrary[String]
  } yield Consumer(name)

  implicit lazy val arbConsumer:Arbitrary[Consumer] = Arbitrary(genConsumer)

  property("some prop") = forAll { c:Consumer =>
    // Check stuff
    true
  }
}
于 2014-02-18T01:16:59.537 回答