abstract trait MyApi {
def getResult()(implicit ec: ExecutionContext): Future[String]
}
以下不有效:
val m = mock[MyApi]
(m.getResult _).expects() returning "..."
失败了:
java.lang.ClassCastException: org.scalamock.MockFunction1 cannot be cast to org.scalamock.MockFunction0
注意:http://scalamock.org/user-guide/advanced_topics/中给出的示例仅在方法至少有一个参数时才有用。所以我们不能像mocking methods which use ClassTag in scala using scalamock
那样使用解决方案答案 0 :(得分:6)
我猜你没看正确的例子。请看示例4中的隐式参数:
class Codec()
trait Memcached {
def get(key: String)(implicit codec: Codec): Option[Int]
}
val memcachedMock = mock[Memcached]
implicit val codec = new Codec
(memcachedMock.get(_ : String)(_ : Codec)).expects("some_key", *).returning(Some(123))
在你的情况下,当然,非隐式参数是null,所以你想要:
(m.getResult()(_: ExecutionContext)).expects(*) returning "..."