在specs2中,我可以通过以下方式验证方法的调用次数:
there was one(user).getName
there were two(user).getName
there were three(user).getName
但如何检查N次?我正在寻找的东西就像:
there were times(n, user).getName
不幸的是没有这样的API
答案 0 :(得分:1)
如果你看看mockto的specs2绑定的实现,你会发现像
这样的东西def one[T <: AnyRef](mock: T)(implicit anOrder: Option[InOrder] = inOrder()): T =
verify(mock, org.mockito.Mockito.times(1))(anOrder)
所以我猜你可以通过模仿那个来定义你自己的times
方法:
def times[T <: AnyRef](n: Int, mock: T)(implicit anOrder: Option[InOrder] = inOrder()): T =
verify(mock, org.mockito.Mockito.times(n))(anOrder)
或者只是明确地使用mockito:
val mocker = new MockitoMocker {}
verify(user, org.mockito.Mockito.times(42)).getName
答案 1 :(得分:1)
这是一个遗漏。我刚刚向API添加了exactly
并暂时将其发布为specs2-mock-2.4.7-SNAPSHOT(很快就会发布正式版):
val list2 = mock[java.util.List[String]]
1 to 2 foreach { i => list.add("one") }
there was exactly(2)(list).add("one")