2017-05-07 72 views
3

我试图按照this doc - scalatestscala-mock嘲笑功能并检查它是否已被称为斯卡拉模拟,MockFactory预计将失败,并“意外来电”

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" 

回答

3

你需要调用mockCombop.expectsmockCombop之前被调用,而不是之后:

"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) 
} 
+0

呀。接得好。唯一的事情就是,“底线”要比底线好得多。 – ses

+0

您可以使用Record-then-Verify样式,如下所述:http://scalamock.org/user-guide/mocking_style/ –

+0

https://github.com/Sergey80/scala-samples/blob/master/algorithms/的src /测试/阶/测试/ AggregateSpec.scala – ses