我正在尝试遵循此文档 - scalatest
scala-mock
来模拟函数并检查它是否已被调用
class AggregateSpec extends FlatSpec with Matchers with MockFactory {
val data = Seq("This", "is", "something", "I", "would", "like", "to", "know")
"combop function" should "BE called for par collection" in {
val mockCombop = mockFunction[Int, Int, Int]
val parData = data.par
val result: Int = parData.aggregate(0)(
seqop = (acc, next) => acc + next.length,
combop = mockCombop
)
result should === (31)
mockCombop.expects(*, *).atLeastOnce()
}
}
结果:
> [info] - should BE called for non-par collection *** FAILED *** [info]
> Unexpected call: MockFunction2-1(4, 2) [info] [info] Expected:
> [info] inAnyOrder { [info] [info] } [info] [info] Actual:
> [info] MockFunction2-1(9, 1) [info] MockFunction2-1(2, 4)
> [info] MockFunction2-1(4, 2) [info] MockFunction2-1(5, 4)
> (Option.scala:121)
为什么?如何让它通过 scalatest + scala-mock ?
--
作为部门,我使用:
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.1",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test",
libraryDependencies += "org.scalamock" %% "scalamock-scalatest-support" % "3.5.0"
您需要在
调用mockCombop
之前调用mockCombop.expects
,而不是在以下之后:
"combop function" should "BE called for par collection" in {
val mockCombop = mockFunction[Int, Int, Int]
val parData = data.par
mockCombop.expects(*, *).atLeastOnce()
val result: Int = parData.aggregate(0)(
seqop = (acc, next) => acc + next.length,
combop = mockCombop
)
result should === (31)
}