Flutter GoRouter testing strategy for complex navigation using getIt and mocktail

Giovanni Accetta
7 min readDec 16, 2023
Photo by Manouchehr Hejazi on Unsplash

I’ve authored a comprehensive guide on building a scalable Flutter app, [available here]. In its fourth section [available here], I delve into the intricacies of complex navigation using GoRouter and provide insights into testing your app, including coverage for URL navigation and redirection.

I believe this specific aspect is noteworthy enough to warrant a dedicated, condensed article.

GoRouter stands out as a potent declarative routing solution for Flutter. Leveraging a router represents the most advanced approach to fully capitalize on Navigator 2.0, effortlessly supporting deep links, URL navigation, and redirection. GoRouter significantly simplifies the utilization of the Navigator 2.0 API.

However, when it comes to documenting unit tests, some scenarios may not have an immediately obvious approach.

Consider the following route graph for illustration:

ROUTER CONFIG
|
|---DefaultRoute
| Path: "/"
| Redirect (always): "/articles"
|
|---HomeView (StatefulShellRoute)
|
|---Branch 0
| |
| |---ArticleList
| | Path: "/articles?articleId=[articleId]"
| |
| |---ArticleDetails
| Path: "/articles/:articleId"
|…

--

--

Giovanni Accetta

Senior Mobile Developer πŸ§‘β€πŸ’» and freelancer πŸš€ with 360 experience on mobile apps lifecycle πŸ“±! Always happy to talk tech πŸ’», photography πŸ“· and travel ✈️!