4

私はこのドキュメントに従おうとしています-関数scalatestscala-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 で渡す方法は?

--

私が使用するdepsとして:

   libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.1",
    libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test",
    libraryDependencies += "org.scalamock" %% "scalamock-scalatest-support" % "3.5.0"
4

1 に答える 1

5

後ではなく、呼び出されるmockCombop.expects前に呼び出す必要があります。mockCombop

"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)
}
于 2017-05-07T20:40:45.510 に答える