首页 > 解决方案 > Flutter 测试:InputChip 点击删除

问题描述

我在我的应用程序中使用InputChips

InputChip(
  key: Key(label.id),
  label: Text(label.name),
  onDeleted: () => deleteChip(),
),

我现在想创建单元测试来测试删除功能。不幸的是,我发现在单元测试中无法按下芯片的删除图标。有了await tester.tap(find.byKey(Key('1')));它就可以找到芯片。我现在如何定义我要单击芯片上的删除图标?

标签: unit-testingflutterdart

解决方案


您的取景器正在寻找并点击它InputChip本身,而不是芯片上的“X”。

您的取景器应如下所示:

find.descendant(
      of: find.byKey(Key(label.id)),
      matching: find.byIcon(Icons.cancel),
    );

InputChip这说“找到谁是的后代cancel Icon。然后你可以点击它,如果你愿意:)

当您缺少点击目标,或者不确定测试中发生了什么时,请在运行后查看控制台debugDumpApp();


推荐阅读