Testing Can';t在颤振测试中输入文本
我一直在尝试测试一个元素,当按下IconButton时,文本对象将更改为TextFormField。当我尝试测试时,会出现以下错误:Testing Can';t在颤振测试中输入文本,testing,dart,flutter,flutter-test,Testing,Dart,Flutter,Flutter Test,我一直在尝试测试一个元素,当按下IconButton时,文本对象将更改为TextFormField。当我尝试测试时,会出现以下错误: A Timer is still pending even after the widget tree was disposed. 'package:flutter_test/src/binding.dart': Failed assertion: line 672 pos 7: '_fakeAsync.nonPeriodicTimerCount == 0' 尽
A Timer is still pending even after the widget tree was disposed.
'package:flutter_test/src/binding.dart': Failed assertion: line 672 pos 7:
'_fakeAsync.nonPeriodicTimerCount == 0'
尽管小部件没有使用我知道的任何计时器
示例代码:
await tester.tap(find.byType(IconButton));
await tester.pump();
expect(find.byType(TextFormField), findsOneWidget);
await tester.pump();
await tester.enterText(find.byType(EditableText), "0.2");
我真的不确定是什么导致了这个问题,但是如果我删除了最后两行,它运行得很好(尽管我没有实际测试我的输入)。分配给EditableText
new EditableText(
key: new Key('mySpecialEditableText1234'),
controller: myCtrl,
style: myStyle,
focusNode: myFocusNode,
)
并通过以下方式访问测试中的此小部件:
find.byKey(new Key('mySpecialEditableText1234'))
如果没有找到您的小部件,则您的测试仪实例可能没有泵送正确的小部件
注意:确保您通过正确的导航路径。您不能直接泵送不是树中第一个的小部件。问题是使用的文本字段有300毫秒的延迟,以便键盘可以滚动到视图中,然后应用程序可以滚动到文本字段。我完全忽略了文本字段有这种延迟,并且没有解释它
添加
await tester.pump(Duration(milliseconds:400));
完全按照预期工作。测试人员实例发现小部件很好,我将发布解决方案作为另一个答案。