You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SyncFusion Calendar Flutter组件测试遇阻,寻求问题排查方案

解决SyncFusion Calendar Flutter组件Widget测试的问题

问题1:无法找到“No selected date”文本

针对expect(find.text('No selected date'), findsOneWidget)测试失败的情况,可从以下几点调整:

  • 等待组件完全初始化:SyncFusion Calendar的渲染包含异步逻辑,替换await tester.pump()为await tester.pumpAndSettle(),确保所有初始化动画、异步操作完成后再断言:

    await tester.pumpWidget(MaterialApp(
        title: 'Firestore test', home: Calendar(firestore: firestore)));
    
    await tester.idle();
    // 等待所有异步渲染和动画完成
    await tester.pumpAndSettle();
    
    expect(find.text('Appointments'), findsOneWidget);
    expect(find.text('No selected date'), findsOneWidget);
    
  • 检查文本是否被隐藏:如果文本被包裹在Offstage或Visibility组件中,关闭skipOffstage参数:

    expect(find.text('No selected date', skipOffstage: false), findsOneWidget);
    
  • 精确查找组件:若直接查找文本无效,用Widget谓词精准定位:

    expect(find.byWidgetPredicate((widget) => 
      widget is Text && 
      widget.data == 'No selected date'), 
    findsOneWidget);
    

问题2:点击日期无反应

核心是定位正确的可点击组件,而非直接点击文本:

  • 不要直接点击日期文本:SyncFusion的日期单元格是自定义组件(如CalendarCell),文本节点不可点击。推荐给单元格设置唯一Key:
    在Calendar的cellBuilder中配置:

    SfCalendar(
      cellBuilder: (context, details) {
        return Container(
          key: Key('calendar-cell-${details.date.day}'),
          child: Text(details.date.day.toString()),
        );
      },
      // 其他配置项
    )
    

    测试时通过Key定位并点击:

    await tester.tap(find.byKey(Key('calendar-cell-${datetime.day}')));
    
  • 点击后等待状态更新:点击日期后的状态更新需要异步完成,执行await tester.pumpAndSettle()等待UI重绘:

    await tester.tap(yourCalendarCellFinder);
    await tester.pumpAndSettle();
    
    expect(find.text('Appointments'), findsOneWidget);
    expect(find.text('Mark', skipOffstage: false), findsOneWidget);
    
  • 确保Firestore数据加载完成:若日程数据来自Firestore Stream,测试时需确认Stream已发出数据。可使用StreamController手动控制数据发射,避免依赖真实Firestore的异步延迟。


内容的提问来源于stack exchange,提问作者Momo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:01:18