Mockito Scala Specs2模拟复杂功能(Play Framework)

时间:2016-03-14 18:59:09

标签: scala mockito specs2

我正在尝试正确存根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]

问题是,是否可以使用匹配器???零件,特别是对于currying参数部分。

CacheApi函数的返回类型应为Future [Seq [Object]]。

公共git repo链接:Github

1 个答案:

答案 0 :(得分:2)

这有效

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
    }
  }

}