How to test for a specific ProviderNotFoundException in widget test

Issue

I have some providers (e.g provider A,B,C) created above a certain widget and testing the ProviderNotFoundException in the case that a provider was removed from the widget tree.

Right now I’m testing the ProviderNotFoundException like this:

expect(tester.takeException(), isA<ProviderNotFoundException>());

This is very generic because this will work for all the Providers A,B and C if they were removed. I would like to add additional constraints to check if only Provider B was not found.

I tried using the having method on the isA but could not get it to work.

expect(
              tester.takeException(),
              isA<ProviderNotFoundException>().having(
                (e) => e.valueType,
                'valueType',
                contains('A'),
              ));

Solution

Using the having method to add additional constraints and check if the feature argument matches the matcher argument correctly.

 expect(
              tester.takeException(),
              isA<ProviderNotFoundException>().having(
                (e) => e.valueType,
                'valueType',
                A,
              ));

Answered By – Calvin Gonsalves

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *