0

以下代码:

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

给出一个编译错误“使用替代方法验证重载方法值:(matcher: org.scalamock.FunctionAdapter1[Int,Booleam]) org.scalamock.CallHandler1[Int,Unit] with org.scalamock.Verify (v1: org.scalamock.MockParameter [Int]") org.scalamock.CallHandler1[Int,Unit] 和 org.scalamock.Verify 不能应用于 (xyContainer[Int])"

删除泛型没有帮助。从 stub/verify 切换到 mock/expects 会产生相同的编译错误。

我正在使用 scala 2.11.0

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

非常感谢任何帮助。

4

1 回答 1

2

您的“验证”方法类型错误,它应该是 Int。stubService 是 Service[Int] 的类型,并且 container 也是 Container[Int] => 在 DoesMatch 中您将 Int 传递给服务

(stubService.doWork _).verify(container.value)
于 2014-11-15T21:55:19.287 回答