mardi 27 octobre 2020

Mock a Widget in Flutter tests

I am trying to create tests for my Flutter application. Simple example:

class MyWidget extends StatelessWidget {
   @override
   build(BuildContext context) {
      return MySecondWidget();
   }
}

I would like to verify that MyWidget is actually calling MySecondWidget without building MySecondWidget.

void main() {
   testWidgets('It should call MySecondWidget', (WidgetTester tester) async {
      await tester.pumpWidget(MyWidget());
      expect(find.byType(MySecondWidget), findsOneWidget);
   }
}

In my case this will not work because MySecondWidget needs some specific and complex setup (like an API key, a value in a Provider...). What I would like is to "mock" MySecondWidget to be an empty Container (for example) so it doesn't raise any error during the test.

How can I do something like that ?

Aucun commentaire:

Enregistrer un commentaire