Arnoodles
Arnoodles

Reputation: 169

How to create a widget test that uses go_router's StatefulNavigationShell

I'm trying to write a widget test for a screen that uses go_router's StatefulNavigationShell, but I'm having trouble mocking the router and getting the expected results.

This is the screen that uses StatefulNavigationShell and I want to do a widget test

class MainScreen extends StatelessWidget {
const MainScreen({
    required this.navigationShell,
    super.key,
});

final StatefulNavigationShell navigationShell;

@override
Widget build(BuildContext context) => Scaffold(
      body: Center(
        child: navigationShell,
      ),
      bottomNavigationBar: CustomNavBar(
        navigationShell: navigationShell,
      ),
    ),
  );
}

For additional context this is an example of how I implement StatefulNavigationShell in my project

Upvotes: 4

Views: 385

Answers (1)

xiao66
xiao66

Reputation: 1

I used GenerateNiceMock from mockito package and it works for me. Something like

import 'package:mockito/annotations.dart';
import 'package:go_router/go_router.dart';

@GenerateNiceMocks([MockSpec<StatefulNavigationShell>()])
void main(){
// your test implementation
final navigationShell = MockStatefulNavigationShell();
}

you need to run cmd to get the reference for MockStateFulNavigationShell() from annotation after you save your changes, the command is, dart run build_runner build

Then you can pass in the navigationShell into the constructor for your view

Upvotes: 0

Related Questions