我需要将函数从任意类型模拟为另一种类型,即value-class。例如具有以下签名String => ValueClass
。这就是我的价值类的实现方式:
final case class ValueClass(value: String) extends AnyVal
我正在使用mockito-scala来模拟和scalatest以匹配结果:
import org.mockito.{ArgumentMatchersSugar, MockitoSugar}
import org.scalatest.{Matchers, WordSpecLike}
class ValueClassMockTest
extends WordSpecLike
with MockitoSugar
with ArgumentMatchersSugar
with Matchers {
"mocked function" should {
"return the same value class" in {
val f: String => ValueClass = mock[String => ValueClass]
when(f(any[String])).thenReturn(ValueClass("str"))
f("anyStringValue") shouldEqual ValueClass("str")
}
}
}
我想知道,为什么这个测试没有通过并且模拟函数返回未包装的值?
"str" did not equal ValueClass(str)
ScalaTestFailureLocation: services.ValueClassMockTest at (ValueClassMockTest.scala:16)
Expected :ValueClass(str)
Actual :"str"
<Click to see difference>
构建.sbt
scalaVersion = "2.12.10"
scalaTest = "3.0.8"
mockitoScala = "1.5.17"