我一直在尝试测试一个元素,当按下 IconButton 时,Text 对象会更改为 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'
尽管小部件没有使用任何我知道的计时器?
示例代码:
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");
我真的不确定是什么导致了这个问题,但如果我删除最后两行它运行正常(尽管我没有真正测试我的输入)。
最佳答案
问题是所使用的文本字段有 300 毫秒的延迟,因此键盘可以滚动到 View 中,然后应用可以滚动到文本字段。我完全忽略了文本字段有这种延迟并且没有考虑到它。
添加
await tester.pump(Duration(milliseconds:400));
完全按预期工作。
关于testing - flutter测试无法输入文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50034957/