当然,我必须忽略一些事情,但这里正在发生的事情
object MenuTreeArbitrary extends Properties("MenuTree") {
lazy val genMenuDetails: Gen[MenuDetails] = for {
id <- arbitrary[Int]
label <- arbitrary[String]
itemType <- arbitrary[String]
formId <- arbitrary[Int]
ms <- arbitrary[String]
mv<- arbitrary[JsValue]
items <- Gen.lzy(genMenuDetails)
} yield MenuDetails(Some(id), label, itemType, Some(formId), Some(Map(ms->mv)), Some(List(items)))
implicit lazy val menuDetailsArb: Arbitrary[MenuDetails] = Arbitrary(genMenuDetails)
lazy val genMenuTree: Gen[MenuTree] = for {
id <- arbitrary[Int]
rootOrgId <- arbitrary[Int]
isForMobile <- arbitrary[Boolean]
otherJson <- Gen.lzy(arbitrary[MenuDetails])
} yield MenuTree(id, Some(rootOrgId), isForMobile, otherJson)
}
测试是
test("using generators "){
forAll(genMenuTree) { m: MenuTree =>
val json = Json.toJson(m)
val mt = Json.fromJson[MenuTree](json)
val t = mt match{
case res : JsSuccess[MenuTree] => res.get
}
t should be (m)
}
}
在执行forAll(genMenuTree)时,虽然代码使用的是Gen.lzy,但它会在递归调用时抛出OutOfMemory。有什么想法吗?
rg.scalatest.exceptions.GeneratorDrivenPropertyCheckFailedException: Exception "java.lang.StackOverflowError" (included as the TestFailedException's cause) was thrown during argument generation.
at org.scalatest.prop.Checkers$.doCheck(Checkers.scala:453)
at org.scalatest.prop.GeneratorDrivenPropertyChecks$class.forAll(GeneratorDrivenPropertyChecks.scala:915)