Я создал виджет (GirafNotifyDialog), который использует AlertDialog. Виджет состоит из некоторого текста и единственной кнопки, которая закрывает диалог при нажатии.
При тестировании виджета я пытаюсь подтвердить, что виджет исчезает после нажатия кнопки, но тест не проходит, и я попробовал несколько других подходов, но ни один из них не работает.
Код для этой проблемы можно увидеть в этой проблеме 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)