【发布时间】:2014-07-14 19:05:50
【问题描述】:
我想在 Scala 中使用 Mockito 存根返回 AnyVal 的函数(准确地说是 Specs2),但它无法正常工作:
import org.specs2.mutable._
import org.specs2.mock._
case class V(s: String) extends AnyVal
class A {
def f: V = new V("Hello")
}
class Sample extends Specification with Mockito {
"Mockito" should {
"mock A" in {
val a = mock[A]
a.f returns new V("hoge")
a.f match {
case V("hoge") => success
case _ => failure
}
}
}
}
这失败了:
V cannot be returned by f()
f() should return String
我找到了一种使用标记接口/特征的解决方法(基于我在 sn-p 上面提供的): https://gist.github.com/mtgto/9251779
但这对我来说不是任何解决方案,因此它会更改返回类型(因为模拟/测试库问题)。
任何人以前见过这个并且知道如何存根这样的函数?
【问题讨论】: