Freewind
Freewind

Reputation: 198318

How to verify a method is called N times exactly in specs2?

In specs2, I can verify the invocation times of a method by:

there was one(user).getName
there were two(user).getName
there were three(user).getName

But how to check N times? Something I'm looking for is like:

there were times(n, user).getName

Unlucky there is no such API

Upvotes: 2

Views: 3571

Answers (2)

Eric
Eric

Reputation: 15557

This is an omission. I have just added exactly to the API and published it as specs2-mock-2.4.7-SNAPSHOT for now (an official release should follow soon):

val list2 = mock[java.util.List[String]]
1 to 2 foreach { i => list.add("one") }

there was exactly(2)(list).add("one")

Upvotes: 1

Gabriele Petronella
Gabriele Petronella

Reputation: 108159

If you look at the implementation of the specs2 bindings for mockito, you'll find something like

def one[T <: AnyRef](mock: T)(implicit anOrder: Option[InOrder] = inOrder()): T =
  verify(mock, org.mockito.Mockito.times(1))(anOrder)

So I guess you can just define your own times method, by mimicking that one:

def times[T <: AnyRef](n: Int, mock: T)(implicit anOrder: Option[InOrder] = inOrder()): T =
  verify(mock, org.mockito.Mockito.times(n))(anOrder)

or simply use mockito explicitly:

val mocker = new MockitoMocker {}
verify(user, org.mockito.Mockito.times(42)).getName

Upvotes: 1

Related Questions