在以下情况下,为何flutter-test无法渲染Text小部件?

时间:2019-04-15 20:09:56

标签: dart flutter flutter-test

我有一个名为GiftGrid的小部件,当它没有收到任何物品时显示一个占位符Text小部件,但是检查特定情况的测试失败了,即使它在应用程序中工作正常:< / p>

testWidgets('Null list shows placeholder', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
        localizationsDelegates: [MyLocalizationsDelegate()],
        home: GiftGrid(null)));
    var a = tester.allWidgets.toList();
    expect(find.text("PROPER_QUADRUPLE_CHECKED_STRING"), findsOneWidget);
  });

其中GiftGrid的定义如下:

return (widget.giftList?.isEmpty ?? true)
        ? Center(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(MyLocalizations.of(context).stringThatTranslatesToSameAsTest),
            ),
          ) : StaggeredGridBuilder(...);

调试器显示a的内容如下(很抱歉,屏幕截图,我找不到更好的显示方式): Contents of a as defined in test case

编辑:GifGrid接收列表作为参数,并通过以下测试:

  testWidgets('Gift shows thumbnail if available', (WidgetTester tester) async {
    var gift = GiftModel()
      ..images = [
        "OTHER_IMG_LINK"
      ]
      ..thumbnail =
          "IMG_LINK";

    // Build our app and trigger a frame.
    await tester.pumpWidget(MaterialApp(
      home: GiftGrid([gift]),
    ));

    expect(find.byWidgetPredicate((widget) {
      return widget is TransitionToImage &&
          (widget.image as AdvancedNetworkImage).url ==
              "IMG_LINK";
    }), findsOneWidget);
  });

0 个答案:

没有答案