Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito reset call count?

I am effectively calling a method of a mocked class 3 times in a test but when I assert that the call was method 3 times, the test fail. The actual call count is down to 1 according to the result from 2. How does mockito counts calls ?

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
class RealService {
  doSomething(){

  }
}
class MockService extends Mock implements RealService{}
void main() {
  test('Mockito callcount 3 times',(){
    final mock = MockService();
    mock.doSomething();
    mock.doSomething();
    verify(mock.doSomething()).called(2);
    // now calling a third time again
    mock.doSomething();
    verify(mock.doSomething()).called(3);
  });
}

My background is from sinon in Node

Expected: <3>
  Actual: <1>
Unexpected number of calls
like image 550
TSR Avatar asked Sep 05 '25 02:09

TSR


2 Answers

Mockito has a reset function that does exactly this.

like image 176
oltman Avatar answered Sep 07 '25 01:09

oltman


Use clearInteractions to clear just the call count, reset clears call count and stubs, so you'll need to restub.

like image 31
Nelson Yeung Avatar answered Sep 07 '25 01:09

Nelson Yeung