2016-09-02 1 views
0

Как получить счетчик вызываний на издеваемом объекте?OCMockito получает кол-во приглашений на макет

В определенный момент теста я хотел бы получить текущий счетчик вызовов для определенного метода, затем продолжить тест и, наконец, подтвердить, что метод был вызван еще раз.

Это было бы что-то вроде:

[given([mockA interestingMethod]) willReturnInt:5]; 
<do some work that may call 'interestingMethod' one or two times> 
NSInteger count = currentCountOfInvocations([mockA interestingMethod]); //or something similar 
<do some more work that [hopefully] calls interesting method one more time> 
[verifyCount(mockA, times(count + 1)) interestingMethod]; 

ответ

0

Вы можете издеваться ничего с блоком. Итак, давайте использовать блок, чтобы увеличить наш счетчик.

__block NSUInteger interestingMethodCount = 0; 
[given([mockA interestingMethod]) willDo:^id(NSInvocation *invocation) { 
    interestingMethodCount += 1; 
    return @5; 
}]; 

<do some work that may call 'interestingMethod' one or two times> 
NSUInteger countAtCheckpoint = interestingMethodCount; 

<do some more work that [hopefully] calls 'interestingMethod' one more time> 
assertThat(@(interestingMethodCount), is(equalTo(@(countAtCheckpoint + 1))));