Mockito Scala Specs2 模拟复杂函数(Play Framework)
Mockito Scala Specs2 mocking complex functions (Play Framework)
我正在尝试正确存根 Play Framework 使用的 ehCache API。特别是,其带有签名的 getOrElse 函数:
def getOrElse[A: ClassTag](key: String, expiration: Duration)(orElse: => A)
在我的规范 2 代码中,我有:
val mockCache = mock[EhCacheApi]
mockCache.getOrElse[???](anyString,anyObject[Duration])(???) returns
[Object I'd like returned]
问题是是否可以为 ??? 使用匹配器部分,特别是柯里化参数部分。
CacheApi 函数的 return 类型应该是 Future[Seq[Object]] 。
Public git 回购 link: Github
这个有效
class VariationAssignmentSpec(implicit ee: ExecutionEnv) extends PlaySpecification with Mockito {
case class Variation(id: Option[Long] = None)
lazy val v1 = Variation(Option(1L))
lazy val v2 = Variation(Option(2L))
"Cache#getOrElse" should {
"return correct result" in {
val mockCache = mock[CacheApi]
mockCache.getOrElse[Future[Seq[Variation]]](anyString, any[Duration])(any)(any) returns
Future(Seq(v1, v2))
val resultFuture: Future[Seq[Variation]] =
mockCache.getOrElse("cache.key", 10.seconds)(Future(Seq(v1,v2)))
resultFuture must equalTo(Seq(v1,v2)).await
}
}
}
我正在尝试正确存根 Play Framework 使用的 ehCache API。特别是,其带有签名的 getOrElse 函数:
def getOrElse[A: ClassTag](key: String, expiration: Duration)(orElse: => A)
在我的规范 2 代码中,我有:
val mockCache = mock[EhCacheApi]
mockCache.getOrElse[???](anyString,anyObject[Duration])(???) returns
[Object I'd like returned]
问题是是否可以为 ??? 使用匹配器部分,特别是柯里化参数部分。
CacheApi 函数的 return 类型应该是 Future[Seq[Object]] 。
Public git 回购 link: Github
这个有效
class VariationAssignmentSpec(implicit ee: ExecutionEnv) extends PlaySpecification with Mockito {
case class Variation(id: Option[Long] = None)
lazy val v1 = Variation(Option(1L))
lazy val v2 = Variation(Option(2L))
"Cache#getOrElse" should {
"return correct result" in {
val mockCache = mock[CacheApi]
mockCache.getOrElse[Future[Seq[Variation]]](anyString, any[Duration])(any)(any) returns
Future(Seq(v1, v2))
val resultFuture: Future[Seq[Variation]] =
mockCache.getOrElse("cache.key", 10.seconds)(Future(Seq(v1,v2)))
resultFuture must equalTo(Seq(v1,v2)).await
}
}
}