2021年1月5日星期二

flutter riverpod: how to test asyncvalue.error?

I am having trouble testing my Riverpod FutureProvider when it returns an AsyncValue.error.

I tried the test as shown below:

// Future provider    final ipsProvider = FutureProvider.autoDispose((_) =>  IpRepository().fetchIps());    // Widget to be tested  class ExampleWidget extends ConsumerWidget {    @override    Widget build(BuildContext context, ScopedReader watch) {      // hooks      AsyncValue<List<Ip>> ips = watch(ipsProvider);      return Container(        child: ips.when(          data: (data) => randomWidget,          loading: () => progressIndicator,          error: (_, stack) => Text('YesMan'),        ),      );    }  }    // Test: I am trying to find a Text widget containing message 'YesMan'  testWidgets('ExampleWidget | error', (WidgetTester tester) async     await tester.pumpWidget(          ProviderScope(              overrides: [                ipsProvider.overrideWithValue(AsyncValue.error('randomErrorMessage')),              ],              child: MaterialApp(                home: Builder(builder: (context) {                  return ExampleWidget();                }),              ),            ),        );        final finderError = find.text('YesMan');        expect(finderError, findsOneWidget);      });  

I expected the test to return a text widget with message 'randomError', but instead it throws an exception as below:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK The following message was thrown running a test: randomErrorMessage When the exception was thrown, this was the stack:

Any idea on how to test AsyncValue.error cases in Riverpod?

Thank you

https://stackoverflow.com/questions/65554979/flutter-riverpod-how-to-test-asyncvalue-error January 04, 2021 at 05:11AM

没有评论:

发表评论