ScalaMock 验证通用方法返回单元会导致方法重载编译错误



以下代码:

import org.scalamock.scalatest.MockFactory
import org.scalatest.FlatSpec
case class Container[T](value: T)
trait Service[T] {
  def doWork(value: T): Unit
}
class DoesMatch[T](service: Service[T]) {
  def doMatch(container: Container[T]) = container match {
    case Container(value) => service.doWork(value)
  } 
}
class TestScalaMock extends FlatSpec with MockFactory {
  "Scala Mock" should "verify my call" in {
    val stubService = stub[Service[Int]]
    val matcher = new DoesMatch[Int](stubService)
    val container = Container(2)
    matcher.doMatch(container)
    (stubService.doWork _).verify(container)
  }
}

给出一个编译错误"重载的方法值验证与替代项:(匹配器:org.scalamock.FunctionAdapter1[Int,Booleam

]) org.scalamock.CallHandler1[Int,Unit] with org.scalamock.Verify (v1: org.scalamock.MockParameter[Int]") org.scalamock.CallHandler1[Int,Unit] with org.scalamock.Verify 不能应用于 (x.y.Container[Int])"

删除泛型无济于事。从存根/验证交换到模拟/期望会产生相同的样式编译错误。

我正在使用 scala 2.11.0 与

  "org.scalatest" % "scalatest_2.11" % "2.2.1" % "test",
  "org.scalamock" %% "scalamock-scalatest-support" % "3.2" % "test"

任何帮助非常感谢。

你的"验证"方法的类型错误,它应该是 Int.stubService 是 Service[Int] 的类型,容器也是 Container[Int] => 在 DoesMatch 中,您将 Int 传递给服务

(stubService.doWork _).verify(container.value)

相关内容

  • 没有找到相关文章

最新更新