简体   繁体   English

找不到小部件键抖动测试

[英]Can't found widget key flutter test

So, i'm trying to test my flutter app.所以,我正在尝试测试我的颤振应用程序。 Here is what i do这是我所做的

class MockSplashScreenBloc extends MockBloc<SplashScreenState>
    implements SplashScreenBloc {}

void main() {
  MockSplashScreenBloc splashScreenBloc;

  Widget MyWidget() {
    return MaterialApp(
      home: BlocProvider(
        create: (context) {
          return SplashScreenBloc(url: "google.com");
        },
        child: SplashScreen(),
      ),
    );
  }

  group('Splash Screen Widget Test', () {
    setUp(() {
      splashScreenBloc = MockSplashScreenBloc();
    });
    tearDown(() {
      splashScreenBloc?.close();
    });

    testWidgets('should render Container when state is Default State',
        (WidgetTester tester) async {
      when(splashScreenBloc.state).thenAnswer((_) => Default());
      await tester.pumpWidget(MyWidget());
      expect(find.byKey(ValueKey("container_empty")), findsOneWidget);
    });

    testWidgets('should render LoadingIndicator when state is Loading State',
        (WidgetTester tester) async {
      when(splashScreenBloc.state).thenReturn(LoadingState());

      await tester.pumpWidget(MyWidget());

      expect(find.byKey(ValueKey("splash_loading_bar")), findsOneWidget);
    });
  });

}

Here is my SplashScreen这是我的启动SplashScreen

class SplashScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: BlocBuilder<SplashScreenBloc, SplashScreenState>(
          builder: (context, state) {
            if (state is LoadingState) {
              return CircularProgressIndicator(
                key: Key("splash_loading_bar"),
              );
            } else if (state is NotConnected) {
              return Text("Could not connect to server",
                  key: ValueKey("splash_screen_not_connected"));
            } else if (state is Connected) {
              return Text(
                "Connected",
                key: Key("splash_screen_connected"),
              );
            } else {
              return Container(key: Key("container_empty"));
            }
          },
        ),
      ),
    );
  }
}

i can't pass this test should render LoadingIndicator when state is Loading State , i alrady try to use expect(find.byType(CircularProgressIndicator), findsOneWidget); should render LoadingIndicator when state is Loading State ,我无法通过此测试should render LoadingIndicator when state is Loading State ,我已经尝试使用expect(find.byType(CircularProgressIndicator), findsOneWidget); but it is still not working, here is the error但它仍然无法正常工作,这是错误

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following TestFailure object was thrown running a test: Expected: exactly one matching node in the widget tree Actual: _KeyFinder:<zero widgets with key [<'splash_loading_bar'>] (ignoring offstage widgets)> ==╡ 异常被颤振测试框架捕获 ╞=================================== ============ 在运行测试时抛出以下 TestFailure 对象: 预期:小部件树中恰好有一个匹配节点实际:_KeyFinder:<带有键 [<'splash_loading_bar'>] 的零小部件(忽略场外小部件)>
Which: means none were found but one was expected其中:表示没有找到,但预期有一个

how can i fix it ?我该如何解决?

Did you try await tester.pumpAndSettle()?你试过 await tester.pumpAndSettle() 吗?

tester.pump() tester.pump()
Triggers a rebuild of the widget after a given duration.在给定的持续时间后触发小部件的重建。

tester.pumpAndSettle() tester.pumpAndSettle()
Repeatedly calls pump with the given duration until there are no longer any frames scheduled.以给定的持续时间重复调用泵,直到不再有任何安排的帧。 This essentially waits for all animations to complete.这实质上是等待所有动画完成。

Please try the following code :请尝试以下代码:

   testWidgets('should render LoadingIndicator when state is Loading State',
        (WidgetTester tester) async {
      when(splashScreenBloc.state).thenReturn(LoadingState());

      await tester.pumpWidget(MyWidget());

      await tester.pumpAndSettle();

      expect(find.byKey(ValueKey("splash_loading_bar")), findsOneWidget);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dart / flutter 小部件测试,通过键或文本都找不到文本小部件 - Dart / flutter widget test, Text Widgets are not found either by key nor by text Flutter 小部件测试:无法通过语义 Label 找到小部件,尽管它存在于转储中 - Flutter Widget Testing: Can't Find Widget by Semantics Label in Test, Though it's Present in Dump 为什么在我的测试成功登录到我的 flutter 应用程序后,我的集成测试中的小部件查找器无法找到小部件? - why can't the widget finder in my integration test find a widget after my test successfully signs into my flutter app? Flutter 小部件测试未找到已创建的小部件 - Flutter Widget Test Doesn't Find Created Widget Flutter Listview Widget 测试失败并在列表中找不到错误文本? - Flutter Listview Widget test fails with error text not found inside list? Flutter 未找到任何材质小部件 - Flutter No Material widget found 没有找到材料部件 flutter - no material widget found flutter Flutter:未找到 ScaffoldMessenger 小部件 - Flutter : No ScaffoldMessenger widget found Flutter 小部件无法在初始化程序中访问 - Flutter widget can't be accessed in an initializer 在 flutter 中找不到材料小部件 - No Material widget found in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM