flutter riverpod:如何测试 asyncvalue.error?
flutter riverpod: how to test asyncvalue.error?
当我的 Riverpod FutureProvider return 是 AsyncValue.error.
时,我无法测试它
我尝试了如下所示的测试:
// 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);
});
我希望测试 return 带有消息 'randomError' 的文本小部件,但它抛出了如下异常:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK The following message was thrown running a test: randomErrorMessage When the exception was thrown, this was the stack:
知道如何在 Riverpod 中测试 AsyncValue.error 个案例吗?
谢谢
而不是overrideWithValue
尝试overrideProvider
和运行一个future.error所以FutureProvider抓住它
class ExampleWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, ScopedReader watch) {
// hooks
AsyncValue<String> ips = watch(ipsProvider);
return Container(
child: ips.when(
data: (data) => SizedBox(),
loading: () => CircularProgressIndicator(),
error: (_, stack) => Text('YesMan'),
),
);
}
}
testWidgets('ExampleWidget | error', (WidgetTester tester) async {
await tester.pumpWidget(
ProviderScope(
overrides: [
ipsProvider.overrideWithProvider(FutureProvider((_) => Future.error('error'))),
],
child: MaterialApp(
home: Builder(builder: (context) {
return ExampleWidget();
}),
),
),
);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pump();
final finderError = find.text('YesMan');
expect(finderError, findsOneWidget);
});
当我的 Riverpod FutureProvider return 是 AsyncValue.error.
时,我无法测试它我尝试了如下所示的测试:
// 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);
});
我希望测试 return 带有消息 'randomError' 的文本小部件,但它抛出了如下异常:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK The following message was thrown running a test: randomErrorMessage When the exception was thrown, this was the stack:
知道如何在 Riverpod 中测试 AsyncValue.error 个案例吗?
谢谢
而不是overrideWithValue
尝试overrideProvider
和运行一个future.error所以FutureProvider抓住它
class ExampleWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, ScopedReader watch) {
// hooks
AsyncValue<String> ips = watch(ipsProvider);
return Container(
child: ips.when(
data: (data) => SizedBox(),
loading: () => CircularProgressIndicator(),
error: (_, stack) => Text('YesMan'),
),
);
}
}
testWidgets('ExampleWidget | error', (WidgetTester tester) async {
await tester.pumpWidget(
ProviderScope(
overrides: [
ipsProvider.overrideWithProvider(FutureProvider((_) => Future.error('error'))),
],
child: MaterialApp(
home: Builder(builder: (context) {
return ExampleWidget();
}),
),
),
);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pump();
final finderError = find.text('YesMan');
expect(finderError, findsOneWidget);
});