Test futureBuilder widgets

  1. Summary of my problem
    I trying to test my custom widgets which return the widget from a future builder. After the “pump” of the widget, I receive immediately the response instead of waiting the the response.
  • Details about my goal
    I would like to receive the widget after the futureBuilder completed the action.

  • Expected Results
    I expect to find a Text widget in the test

  • Actual Results
    After the pump in the test, I can see there is a Container

  • Error Messages (if any)
    I have no error.

  1. What have I tried so far -

This is my widget to test

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {

@override
  Widget build(BuildContext context) {
    return widgetLoadType == LoadType.Delay
        ? Container()
        : FutureBuilder<Widget>(
            future: getChildren(),    // this function returns the actual widget (could be anything in this case it returns a Text)
            builder: (BuildContext context,
                AsyncSnapshot<Widget> snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Container()        // I receive this widget when I perform the test 
              } else {
                if (snapshot.hasError) {
                  SmeupLogService.writeDebugMessage(
                      'Error $type: ${snapshot.error}',
                      logType: LogType.error);
                  notifyError(context, id, snapshot.error);
                  return SmeupNotAvailable();
                } else {
                  return snapshot.data.children;   // I woul like to receive this response
                }
              }
            },
          );
   }

}

This is my test

 testWidgets('Test futurebuilder widget',
      (WidgetTester tester) async {

final myWidget = MyWidget();
await tester.pumpWidget(myWidget);

// this fails
var labelTextFinder = find.byType(Text);
expect(labelTextFinder, findsWidgets);

// this succedes
// var labelTextFinder = find.byType(Container);
// expect(labelTextFinder, findsWidgets);

}

  1. Here’s the minimum code you would need to reproduce the problem -
    see above

I have found the solution. I have to use:

tester.pumpAndSettle()

instead of

tester.pump()