【发布时间】:2020-05-10 03:45:19
【问题描述】:
我有一个EmailScreen(有状态小部件),它有一个文本输入和一个按钮。该按钮仅在输入有效电子邮件时启用。
我正在使用 Bloc,我的屏幕有 InitialEmailState 和 ValidEmailInputState,当我运行应用程序时它运行良好。
在我的小部件测试中,第二个期望在 bloc 有机会更新状态之前失败:
testWidgets('when valid email is input, button is enabled', (tester) async {
const validEmail = 'email@provider.com';
emailBloc.listen((event) {
print('NEW EVENT: ' + event.toString());
});
await bootUpWidget(tester, emailScreen);
final BottomButton button = tester.widget(
find.widgetWithText(BottomButton, 'CONTINUE'));
expect(button.enabled, isFalse);
await tester.enterText(find.byType(TextInputScreen), validEmail);
await tester.pumpAndSettle();
expect(button.enabled, isTrue);
});
这是我得到的输出:
NEW EVENT: InitialEmailState
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
Expected: true
Actual: <false>
...
The test description was:
when valid email is input, button is enabled
════════════════════════════════════════════════════════════════════════════════════════════════════
Test failed. See exception logs above.
The test description was: when valid email is input, button is enabled
NEW EVENT: InputValidEmailState
✖ when valid email is input, button is enabled
Exited (1)
如您所见,它打印初始状态,第二次期望失败,然后打印期望状态。
提前致谢:)
== 更新 ==
我们通过在 main 开头添加 LiveTestWidgetsFlutterBinding(); 来实现此功能。但这感觉不是一个好的解决方案。
【问题讨论】: