samedi 21 septembre 2019

Flutter Mockito verify that callback passed to widget is called

I have a widget that takes a callback which is called when a button is pressed. I am trying to test that the callback is correctly invoked by the button.

I've tried mocking a Function class:

class MockCallback extends Mock implements Function {
  call() {}
}

Then passing an instance of the mock class to my widget and simulating a tap:

final mocked = MockCallback();
await tester.pumpWidget(
  MyWidget(myCallback: mocked),
);

final clearButtonFinder = find.byType(IconButton);
await tester.tap(clearButtonFinder);

verify(mocked()).called(1);

This results in an error on the verify call saying Used on a non-mockito object. If I put a print statement inside the mocked call, I can see that the tap is indeed calling it.

How can I verify that the callback passed to my widget is getting called once when the button is tapped?

Aucun commentaire:

Enregistrer un commentaire