-
-
Notifications
You must be signed in to change notification settings - Fork 153
How to use this package in the widget test ? #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Did you register |
How to register the AnnouncementBloc in the test? |
in your app you register AnnouncementBloc ? |
This is my simple test. Yes, I registered the block in my app void main() { testWidgets('Exist FloatingActionButton', (WidgetTester tester) async {
}); |
you have to register your objects after the reset |
import 'package:flutter/material.dart'; void main() {
}); Widget createWidgetForTesting({Widget child}) { testWidgets('Exist FloatingActionButton', (WidgetTester tester) async {
}); and my error⬇ Testing started at 4:42 PM ... The relevant error-causing widget was: When the exception was thrown, this was the stack: ════════════════════════════════════════════════════════════════════════════════════════════════════ Test failed. See exception logs above. Please tell me how to register the object correctly in the test |
you have to do the reset and register inside your test like:
|
Hello @escamoteur , More context:
Is it a bug or a feature? Why don't we leverage getIt in the main app? |
The problem is that the main app isn't executed inside of tests. What you can do is calling your registration function of tge main app in the setup function of the tests
Am 1. Jan. 2024, 13:44 +0100 schrieb PHẠM VĂN THẬT ***@***.***>:
… Hello @escamoteur ,
Why do we have to register dependencies again while we do this in the main app?
More context:
1. > main.dart
void main() { registerDependencies(); // Register dependency for GetIt runApp(MyApp()); }
2. > main_test.dart
`void main() {
testWidget('TEST', (tester) async {
await tester.pumpWidget(MyApp());
await tester.pumpAndSettle();
// We can not use GetIt to get this dependency value (we must re-register again before using it)
GetIt.I.get().value;
});
}`
Is it a bug or a feature? Why don't we leverage getIt in the main app?
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Hello Mr, how about when we call the app main function inside testWidget? |
Honestly I do not know if that is possible you have to try
Am 2. Jan. 2024, 11:01 +0100 schrieb PHẠM VĂN THẬT ***@***.***>:
… > The problem is that the main app isn't executed inside of tests. What you can do is calling your registration function of tge main app in the setup function of the tests Am 1. Jan. 2024, 13:44 +0100 schrieb PHẠM VĂN THẬT @.>:
> …
> Hello @escamoteur , Why do we have to register dependencies again while we do this in the main app? More context: 1. > main.dart void main() { registerDependencies(); // Register dependency for GetIt runApp(MyApp()); } 2. > main_test.dart void main() { testWidget('TEST', (tester) async { await tester.pumpWidget(MyApp()); await tester.pumpAndSettle(); // We can not use GetIt to get this dependency value (we must re-register again before using it) GetIt.I.get().value; }); } Is it a bug or a feature? Why don't we leverage getIt in the main app? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.>
Hello Mr, how about when we call the app main function inside testWidget?
testWidget('TEST', (tester) async {
app.main(); // include register GetIt
// we still re-register GetIt.
});
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
You are right. It will work when I use app.main() instead of pumpWidget(MyApp()). |
You did not mention the widget test in the package description.
How can I solve my problem?
It displays this error when I run the widget test.
⬇
Testing started at 10:07 AM ...
C:\src\flutter\bin\flutter.bat --no-color test --machine --plain-name "Exist FloatingActionButton" test\features\announcement\presentation\widgets_test\announcement_widget_test.dart
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞══════════════════════'═════════════════════════════════════
The following assertion was thrown building _BodyBuilder:
No type AnnouncementBloc is registered inside GetIt.
Did you forget to pass an instance name?
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;did you
forget to register it?)
'package:get_it/get_it_impl.dart':
Failed assertion: line 251 pos 14: 'instanceFactory != null'
The relevant error-causing widget was:
Scaffold
file:///C:/Projects/niaz-flutter/lib/features/announcement/presentation/pages/announcements_page.dart:15:12
When the exception was thrown, this was the stack:
#2 _GetItImplementation._findFactoryByNameOrType (package:get_it/get_it_impl.dart:251:14)
#3 _GetItImplementation.get (package:get_it/get_it_impl.dart:263:27)
#4 _GetItImplementation.call (package:get_it/get_it_impl.dart:288:12)
#5 new _AnnouncementsWidgetState (package:niaz/features/announcement/presentation/widgets/announcements_widget.dart:22:19)
#6 AnnouncementsWidget.createState (package:niaz/features/announcement/presentation/widgets/announcements_widget.dart:18:46)
#7 new StatefulElement (package:flutter/src/widgets/framework.dart:4600:25)
#8 StatefulWidget.createElement (package:flutter/src/widgets/framework.dart:896:38)
... Normal element mounting (19 frames)
#27 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14)
#28 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5956:32)
... Normal element mounting (193 frames)
#221 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14)
#222 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5956:32)
... Normal element mounting (255 frames)
#477 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14)
#478 Element.updateChild (package:flutter/src/widgets/framework.dart:3220:20)
#479 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1132:16)
#480 RenderObjectToWidgetElement.update (package:flutter/src/widgets/binding.dart:1110:5)
#481 RenderObjectToWidgetElement.performRebuild (package:flutter/src/widgets/binding.dart:1124:7)
#482 Element.rebuild (package:flutter/src/widgets/framework.dart:4227:5)
#483 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2632:33)
#484 AutomatedTestWidgetsFlutterBinding.drawFrame (package:flutter_test/src/binding.dart:1016:18)
#485 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:284:5)
#486 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1113:15)
#487 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1052:9)
#488 AutomatedTestWidgetsFlutterBinding.pump. (package:flutter_test/src/binding.dart:889:9)
#491 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:72:41)
#492 AutomatedTestWidgetsFlutterBinding.pump (package:flutter_test/src/binding.dart:876:27)
#493 WidgetTester.pumpWidget. (package:flutter_test/src/widget_tester.dart:462:22)
#496 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:72:41)
#497 WidgetTester.pumpWidget (package:flutter_test/src/widget_tester.dart:459:27)
#498 main. (file:///C:/Projects/niaz-flutter/test/features/announcement/presentation/widgets_test/announcement_widget_test.dart:15:18)
#499 main. (file:///C:/Projects/niaz-flutter/test/features/announcement/presentation/widgets_test/announcement_widget_test.dart:14:45)
#500 testWidgets.. (package:flutter_test/src/widget_tester.dart:144:29)
#511 FakeAsync.flushMicrotasks (package:fake_async/fake_async.dart:192:32)
#512 AutomatedTestWidgetsFlutterBinding.runTest. (package:flutter_test/src/binding.dart:1109:17)
#513 AutomatedTestWidgetsFlutterBinding.runTest. (package:flutter_test/src/binding.dart:1097:35)
(elided 31 frames from class _AssertionError, dart:async, and package:stack_trace)
════════════════════════════════════════════════════════════════════════════════════════════════════
Test failed. See exception logs above.
The test description was: Exist FloatingActionButton
The text was updated successfully, but these errors were encountered: