Milad-Akarie / auto_route_library

Flutter route generator
MIT License
1.58k stars 402 forks source link

Write Widget Test with AutoTabsScaffold #1772

Open DerJojo11 opened 11 months ago

DerJojo11 commented 11 months ago

Hey guys!

I need help with writing a widget test when I am using a AutoTabsScaffold. I searched for some examples but there is nearly nothing that explains the Issue i have.

Code example using the AutoTabsScaffold

I have a Widget that is calling the AutoTabs Scaffold. ```dart class HomePage extends StatelessWidget { const HomePage({super.key}); @override Widget build(BuildContext context) { return AutoTabsScaffold( routes: const [ ARoute(), BRoute(), CRoute(), ], bottomNavigationBuilder: (_, tabsRouter) => NavigationBar( selectedIndex: tabsRouter.activeIndex, onDestinationSelected: (index) => tabsRouter.setActiveIndex(index), destinations: BottomNavigation.values .map( (element) => NavigationDestination( icon: Icon(element.icon), label: element.name, ), ) .toList(), ), ); } } ```

In my Widget Test I get the following error:

RouteData operation requested with a context that does not include an RouteData. The context used to retrieve the RouteData must be that of a widget that is a descendant of a AutoRoutePage.

Right now I am using auto_route: 5.0.4

As I understand, it searches for a RouterData, but cannot find one. Of course, I didn't have one in my Widget Test. So I changed my test according to some approaches:

Example of a widget test with RouteData

```dart testWidgets( 'example test', (tester) async { await tester.pumpWidget( StackRouterScope( controller: MockAppRouter(), stateHash: 0, child: const HomePage(), ), ); }, ); ```

When I run this, I still get the error above.. Maybe I am just dump and cant see the solution, but it would be nice when we can get any documentation about how we can use AutoRoute in WidgetTests.

devnta commented 9 months ago

+1. I'm facing same issue.

ajanec01 commented 5 months ago

Hi @DerJojo11 Have you tried TabsRouterScope instead of StackRouterScope?

DerJojo11 commented 3 months ago

Hi @ajanec01 !

Better late than never! :D Yes, I tried it out but still got this error message. Hope there will be a solution someday :).