我跟踪this example是为了测试101是否出现在我的视图中,但即使Flutter 构建树显示小部件存在,我仍然得到以下异常:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
Expected: exactly one matching node in the widget tree
  Actual: _WidgetFinder:<zero widgets with the given widget
(CircularProgressIndicator(<indeterminate>)) (ignoring offstage widgets)>
   Which: means none were found but one was expected

When the exception was thrown, this was the stack:
#4      main.<anonymous closure> (file:///D:/xxxx/xxxx/xxxx/test/widget_test/widget_test.dart:173:5)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

This was caught by the test expectation on the following line:
  file:///D:/xxxx/xxxx/xxxx/test/widget_test/widget_test.dart line 173
The test description was:
  ViewRequest: Waiting Types List
════════════════════════════════════════════════════════════════════════════════════════════════════

Edit

我做了一个方法,只用一个101小部件作为它的"子部件"来构建100:

Widget createMockViewRequest() {
  return MaterialApp(
      title: 'SmartDevice Simulator',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const CircularProgressIndicator());
}

以下测试仍然失败,但有一个例外:

testWidgets('ViewRequest: Waiting Types List', (WidgetTester tester) async {
  const childWidget = CircularProgressIndicator();

  // Build our app and trigger a frame.
  await tester.pumpWidget(createMockViewRequest());

  // Verify that the page is loading until we receive the types.
  expect(find.byWidget(childWidget), findsOneWidget);

  await tester.pumpAndSettle();
});

我做错什么了吗?也许100不算集装箱?但在这种情况下,我不明白为什么不应该这样做.

推荐答案

为了让它发挥作用,有几件事需要改变.

首先,如果在测试带有无限动画(如CircularProgressIndicator)的小部件时使用await tester.pumpAndSettle(),测试将失败,因为它正在等待最后一帧更新,但动画永远不会结束,因此会抛出超时.

除此之外,您的测试失败是因为您正在使用finder,您试图在您的UI中找到与Expect完全相同的实例,但事实并非如此,因为在UI中构建的实例与您在测试中创建的实例不同,在这种情况下,如果您想要判断UI上是否有CircularProgressIndicator,您应该使用find.byType查找器,它在已经构建的小部件中搜索所提供类型的小部件.

Flutter相关问答推荐

Flutter:如何从Bottom Navigator中删除此内容

在Flutter中,有没有一种方法可以点击页面到后面的页面?

在Flutter 中压缩图片返回空

在Flutter 中将参数从一个类传递到另一个类

为什么Riverpod生成器在这种情况下不生成AsyncNotiator?

如何在扩展其他小部件时访问Ref

出现异常.RangeError(RangeError(index):无效值:有效值范围为空:0).分度误差

CMakeLists.txt处的CMake错误:3(项目)找不到指定的Visual Studio实例

将图标呈现在Flutter 克牌小工具的角落上

使用Future函数的容器中未显示图像,但在使用图像小工具时显示

Riverpod 2.0-如何使用FutureOr和AutoDisposeSyncNotificationer处理api状态代码?

Flutter: pie_chart 插件动画滚动问题

Flutter 分析发现 Gitlab CI/CD 管道中的问题,但它在本地没有

type '({bool growable}) => List' 不是类型转换中类型 'List' 的子类型

更改文本字段Flutter 中最大字母的 colored颜色

为什么 Flutter riverpod 直接分配不起作用但方法可以

Flutter:制作自定义小部件的最佳做法是什么?

有任何方法可以在应用程序移动背景恢复时显示 appopenAd

如何使按钮被选中?

Flutter NavigationBar 忽略我的背景 colored颜色