0

有一个类InvokeLater,定义如下:

class InvokeLater {
    def apply(f: => Any): Unit = { 
       // do something ...
       f 
       // do some other thing
    }
}

在规格测试中,我嘲笑它:

val invokeLater = mock[InvokeLater]
invokeLater.apply(any) answers { f => f:Unit }

但似乎里面的代码answers永远不会运行。

specs2 现在支持这个功能吗?

4

1 回答 1

1

首先,您需要确保将specs2-mock.jar它放在mockito.jar您的类路径之前。然后请注意,f传递给该answers方法的是一个Function0. 例如

class InvokeLater {
  def apply(f: =>Int): Unit = {
    // do something ...
    f
    // do some other thing
  }
}

val invokeLater = mock[InvokeLater]

invokeLater.apply(any) answers { f =>
  println("got the value "+f.asInstanceOf[Function0[Int]]())
}

invokeLater.apply(1)

这打印:

got the value 1
于 2015-03-16T01:40:35.267 回答