通常会检查 Specs2 加糖的 Mockito 验证,并在适当的时候通过测试。但是在某些情况下,它们会被忽略。
通常,此测试验证会按预期失败,因为 myApp 至少调用了一次 myService。
import org.specs2.mock.Mockito._
class MySpec extends Specification with Mockito {
"MyApp" should {
"blow up" in WithApplication {
val myService = mock[MyService]
val myApp = new MyApp(myService)
myApp.doSomething
there was no(myService).doSomethingElse
}
}
}
(注意WithApplication是一个玩!框架的东西)
然而,由于我已经将蛋糕模式特征融入到我的组件中,所以我的测试看起来像这样。
class MySpec extends Specification with Mockito {
"MyApp" should {
"blow up" in WithApplication with MockRegistry {
val myApp = new MyApp(myService)
myApp.doSomething
there was no(myService).doSomethingElse
}
}
}
MockRegistry 看起来像这样
trait MockRegistry extends Mockito with MyServiceComponent {
val myService = mock[MyService]
}
我的 Cake 图案测试永远不会失败。我可以将其更改为任何内容,它们都会被忽略。
there was no(myService).doSomethingElse
there was one(myService).doSomethingElse
there was two(myService).doSomethingElse
但是,通过直接调用 java 方法来替换加糖的 mockito 步骤,它在适当的时候确实会失败。
import org.mockito.Mockito._
verify(myService, times(1)).doSomethingElse
因此,似乎涉及测试方法的特征似乎已经正确混淆了 Mockito。