窗口小部件测试未收到水龙头

问题描述

我正在尝试测试自定义窗口小部件的回调,它实际上是一个简单的“保存”图标按钮,但由于窗口小部件未检测到点击,因此测试失败。 测试:

testWidgets(
  'Save icon callback',(WidgetTester tester) async {
    Completer completer = Completer<void>();

    await tester.pumpWidget(
      _TestingApp(
        child: SaveIcon(
          onTap: (_) => completer.complete(),initialSaved: false,),);

    var finder = find.byType(InkWell);
    expect(finder,findsOneWidget);

    await tester.tap(finder);

    expect(completer.isCompleted,true);
  },);

小部件代码:

class SaveIcon extends StatefulWidget {
  SaveIcon({
    @required this.onTap,@required this.initialSaved,});
  final Function(bool) onTap;
  final bool initialSaved;

  @override
  _SaveIconState createState() => _SaveIconState();
}

class _SaveIconState extends State<SaveIcon> {
  bool _isSaved;

  @override
  void initState() {
    super.initState();
    _isSaved = widget.initialSaved;
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      focusColor: Colors.transparent,highlightColor: Colors.transparent,splashColor: Colors.transparent,onTap: () {
        setState(() {
          _isSaved = !_isSaved;
          widget.onTap(_isSaved);
        });
      },child: RotatedBox(
        quarterTurns: 2,child: AnimatedSwitcher(
          transitionBuilder: (child,animation) =>
              ScaleTransition(child: child,scale: animation),duration: Duration(milliseconds: 250),child: _isSaved
              ? Image.asset(
                  'assets/icons/save_filled.png',key: const ValueKey<int>(0),height: 21,color: Colors.white,)
              : Image.asset(
                  'assets/icons/save.png',key: const ValueKey<int>(1),);
  }
}

由于完成程序未完成,它只是失败。 (错误消息:运行测试时引发了以下TestFailure对象: 预期:真 实际:错误 )

知道为什么会这样吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)