2016-09-02 4 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))));