Skip to content
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

How to test widget which calls Routemaster.of(context)? #299

Open
Genora51 opened this issue Aug 4, 2022 · 1 comment
Open

How to test widget which calls Routemaster.of(context)? #299

Genora51 opened this issue Aug 4, 2022 · 1 comment

Comments

@Genora51
Copy link

Genora51 commented Aug 4, 2022

I have a widget containing a button which navigates using routemaster:

ElevatedButton(
  child: const Text("Do Something"),
  onPressed: () {
    // Do some things ...
    Routemaster.of(context).push("/route2");
  },
)

Then, in my widget tests for the widget:

await tester.tap(find.byType(ElevatedButton));
await tester.pump();

However, this will throw an error since the build context has no routemaster in the test widget:

'package:routemaster/routemaster.dart': Failed assertion: line 141 pos 7: 'widget != null': Couldn't get a Routemaster object from the given context.

Is there a way to mock/stub Routemaster for use in widget tests like this?

@tomgilder
Copy link
Owner

Hi, I'd suggest just wrapping your test widgets in a MaterialApp with a Routemaster setup, that's what I do. I always prefer using real dependencies and only mocking when absolutely necessary.

If you don't want to use your real routing setup in the test, you can always inject a fake home route which has just the widget you want to test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants