Skip to content

Testing

Testing is done with overrides. You need to place a ProviderScopeOverride and then specify the overrides argument with a list containing the providers followed by .overrideWithValue(T value).

Examples

The text displayed in the following example is “100” because overrides always take precedence.

testWidgets(
'''ProviderScopeOverride should override providers''',
(tester) async {
final numberProvider = Provider<int>((context) => 0);
await tester.pumpWidget(
ProviderScopeOverride(
overrides: [
numberProvider.overrideWithValue(100),
],
child: MaterialApp(
home: ProviderScope(
providers: [
numberProvider,
],
child: Builder(
builder: (context) {
final number = numberProvider.of(context);
return Text(number.toString());
},
),
),
),
),
);
expect(find.text('100'), findsOneWidget);
});

Testing is possible also with providers that take an argument, and it is done the same exact way.

testWidgets(
'''ProviderScopeOverride should override argument providers''',
(tester) async {
final numberProvider = Provider.withArgument((context, int arg) => arg * 2);
await tester.pumpWidget(
ProviderScopeOverride(
overrides: [
numberProvider.overrideWithValue(8),
],
child: MaterialApp(
home: ProviderScope(
providers: [
numberProvider(1),
],
child: Builder(
builder: (context) {
final number = numberProvider.of(context);
return Text(number.toString());
},
),
),
),
),
);
expect(find.text('8'), findsOneWidget);
});