找不到小部件键颤动测试

Posted

技术标签:

【中文标题】找不到小部件键颤动测试【英文标题】:Can't found widget key flutter test 【发布时间】:2021-02-22 00:12:45 【问题描述】:

所以,我正在尝试测试我的颤振应用程序。这就是我的工作

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);
    );
  );


这是我的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"));
            
          ,
        ),
      ),
    );
  

我无法通过这个测试 should render LoadingIndicator when state is Loading State ,我已经尝试使用 expect(find.byType(CircularProgressIndicator), findsOneWidget); 但它仍然无法正常工作,这是错误

══╡ FLUTTER 测试框架捕获的异常 ╞═════════════════════════════════════════════════ ═══以下 运行测试时抛出了 TestFailure 对象:预期:恰好一个 小部件树中的匹配节点实际:_KeyFinder:](忽略后台小部件)> 其中:表示没有找到,但应该有一个

我该如何解决?

【问题讨论】:

【参考方案1】:

你试过 await tester.pumpAndSettle() 吗?

tester.pump() 在给定的持续时间后触发小部件的重建。

tester.pumpAndSettle() 以给定的持续时间反复调用泵,直到没有 不再安排任何帧。这基本上等待所有动画 完成。

请尝试以下代码:

   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);
    );

【讨论】:

以上是关于找不到小部件键颤动测试的主要内容,如果未能解决你的问题,请参考以下文章

Dart/flutter 小部件测试,无论是按键还是文本都找不到文本小部件

如何模拟用于测试颤动屏幕小部件的导航参数?

如何在颤动中为 CachedNetworkImage 编写小部件测试

测试小部件时在颤动中测试 BlocListener 失败

使颤动的网络图像小部件可点击

颤动垂直抽屉