Flutter 颤振小部件测试找不到InputDecoration.errorText
注意:这是用于颤振web应用程序的 我正在测试一个TextField,以显示如果输入无效,将显示在InputDecoration上指定的错误文本 当我运行整个应用程序并手动与之交互时,小部件可以正常工作 以下是我的文本字段的定义:Flutter 颤振小部件测试找不到InputDecoration.errorText,flutter,flutter-web,flutter-test,Flutter,Flutter Web,Flutter Test,注意:这是用于颤振web应用程序的 我正在测试一个TextField,以显示如果输入无效,将显示在InputDecoration上指定的错误文本 当我运行整个应用程序并手动与之交互时,小部件可以正常工作 以下是我的文本字段的定义: TextField( key: ValueKey('IntSpinner-TEXT'), controller: _controller, keyboardType: _keyboardType(), inputFormatters: widget.i
TextField(
key: ValueKey('IntSpinner-TEXT'),
controller: _controller,
keyboardType: _keyboardType(),
inputFormatters: widget.inputFormatters ?? [formatter],
decoration: InputDecoration(
border: const OutlineInputBorder(),
errorText: _validInput ? null : 'Invalid input',
),
);
我在TextField的控制器上有一个监听器,它检查新值,并相应地将_validInput设置为true或false
下面是我的测试代码:
testWidgets('should flag invalid values', (WidgetTester tester) async {
int x;
await tester.pumpWidget(MaterialApp(
home: Card(
child: Column(
children: [
IntSpinner( // this widget contains my TextField
key: ValueKey('IntSpinner'),
onChanged: (value) => x = value,
),
],
),
),
));
// Enter valid text
await tester.enterText(find.byKey(ValueKey('IntSpinner-TEXT')), '-2200');
expect(x, equals(-2200));
// Enter invalid text
await tester.enterText(find.byKey(ValueKey('IntSpinner-TEXT')), '2-200');
expect(x, equals(-2200)); // unchanged!!
TextField txt = tester.widget(find.byKey(ValueKey('IntSpinner-TEXT')));
expect(txt.decoration.errorText, equals('Invalid input'));
});
我得到这个错误:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
Expected: 'Invalid input'
Actual: <null>
Which: not an <Instance of 'String'>
══╡ 颤振测试框架捕获异常╞════════════════════════════════════════════════════
运行测试时引发了以下TestFailure对象:
应为:“无效输入”
实际:
哪一个:不是一个
我在调试器中看到了同样的情况——inputDecoration.errorText为null。但是,当我在浏览器中运行应用程序时,我可以清楚地看到(使用Flitter Widget Inspector)errorText设置为预期值(我还看到文本字段被绘制为errorText)
小部件检查器输出的片段:
从颤振小部件测试中检查TextField.decoration.errorText的正确方法是什么?我有一个有效的解决方案。我想找个有经验的人来核实一下 当写入测试时设置_validInput时,不会触发小部件的重建。相反,它似乎要等到我“完成”编辑。我通过将焦点从文本字段移到另一个文本字段(使用widgetter.showKeyboard(finder))来假装这一点 现在我的测试步骤如下:
// print('Enter valid text');
await tester.enterText(find.byKey(ValueKey('IntSpinner-TEXT')), '-2200');
expect(x, equals(-2200));
// move the focus somewhere else ... anywhere else
await tester.showKeyboard(find.byKey(Key('FOO')));
TextField txt = tester.widget(find.byKey(ValueKey('IntSpinner-TEXT')));
expect(txt.decoration.errorText, isNull);
// print('Enter invalid text');
await tester.enterText(find.byKey(ValueKey('IntSpinner-TEXT')), '2-200');
expect(x, equals(-2200)); // unchanged!!
// move the focus somewhere else ... anywhere else
await tester.showKeyboard(find.byKey(Key('FOO')));
txt = tester.widget(find.byKey(ValueKey('IntSpinner-TEXT')));
expect(txt.decoration.errorText, equals('Invalid input'));
这让我的测试开始工作;问题是,为什么运行中的应用程序上会显示errorText而不将焦点移动到另一个小部件?您的侦听器是否正确设置了controller.text的初始值,即“”(即,当您未指定初始值且未键入任何内容时)?是的。我实际上提供了一个有效的初始值。更重要的是,我可以直观地检查正在运行的应用程序,就errorText是否可见而言,一切似乎都是正确的。您可以将此测试作为您的第一个测试吗?我怀疑是上面的作业造成的。完成了,没有区别。errorText仍然返回null。这是_validInput的值有问题;我添加了一个打印('build:$\u validInput');语句,它总是打印“build:true”。