我制作了一个使用AlertDialog的小部件(GirafNotifyDialog)。该小部件由一些文本和一个按钮组成,单击该按钮可关闭对话框。 在测试窗口小部件时,我尝试确认按下按钮后窗口小部件消失了,但是测试仍然失败,并且我尝试了其他几种方法,但是似乎都没有用。 这个问题的代码可以在这个github问题上看到: https://github.com/flutter/flutter/issues/30372
对于能够测试该小部件从屏幕上消失的任何帮助,深表感谢!
按下和点击按钮似乎没有什么区别。 我试图测试该小部件是否仍然可见,即使在按下按钮后也是如此。
测试
void main() {
GirafNotifyDialog dialog = GirafNotifyDialog(
title: 'TestingTitle',
description: 'This description is for testing',
);
const String okayBtnKey = 'NotifyDialogOkayButton';
testWidgets('Notify dialog pops on confirmation',
(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: dialog,
)
);
await tester.pump(Duration(milliseconds: 500));
await tester.press(find.byKey(const Key(okayBtnKey)));
await tester.pump(Duration(milliseconds: 500));
expect(find.byType(GirafNotifyDialog), findsNothing);
});
}
和按钮
RaisedButton(
key: const Key('NotifyDialogOkayButton'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
side: const BorderSide(
color: Color.fromRGBO(0, 0, 0, 0.3))),
color: const Color.fromRGBO(255, 157, 0, 1),
child: Row(
children: const <Widget>[
Icon(
Icons.check,
color: Color.fromRGBO(0, 0, 0, 1),
),
Text(
'Okay',
style: TextStyle(color: Colors.white),
),
],
),
onPressed: () {
Navigator.pop(context);
}),
预期结果是该窗口小部件不再可见,但是随着抖动测试响应状态的出现,即使在窗口小部件“应该”消失之后,窗口小部件显然仍然仍然可见。
颤振测试响应:
"C:\Users\marti\Desktop\New folder\flutter\bin\flutter.bat" --no-color test --machine --plain-name "Notify dialog pops on confirmation" test\giraf_notify_dialog_test.dart
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
Expected: no matching nodes in the widget tree
Actual: ?:<exactly one widget with type "GirafNotifyDialog" (ignoring offstage widgets):
GirafNotifyDialog>
Which: means one was found but none were expected
When the exception was thrown, this was the stack:
#4 main.<anonymous closure> (file:///C:/Users/marti/Desktop/FlutterGiraf/weekplanner/test/giraf_notify_dialog_test.dart:25:5)
<asynchronous suspension>
#5 testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:82:23)
#6 TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:571:19)
<asynchronous suspension>
#9 TestWidgetsFlutterBinding._runTest (package:flutter_test/src/binding.dart:555:14)
#10 AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:898:24)
#16 AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test/src/binding.dart:895:15)
#17 testWidgets.<anonymous closure> (package:flutter_test/src/widget_tester.dart:81:22)
#18 Declarer.test.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:168:27)
<asynchronous suspension>
#19 Invoker.waitForOutstandingCallbacks.<anonymous closure> (package:test_api/src/backend/invoker.dart:250:15)
<asynchronous suspension>
#24 Invoker.waitForOutstandingCallbacks (package:test_api/src/backend/invoker.dart:247:5)
#25 Declarer.test.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:166:33)
#30 Declarer.test.<anonymous closure> (package:test_api/src/backend/declarer.dart:165:13)
<asynchronous suspension>
#31 Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/invoker.dart:400:25)
<asynchronous suspension>
#45 _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
#46 _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
#47 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
(elided 28 frames from class _FakeAsync, package dart:async, and package stack_trace)