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)))); 
関連する問題