Sub topic 5
Overriding Providers to Isolate the Code Under Test
Assume you know Dart and Flutter basics, can write package:test or flutter_test tests, and understand Riverpod providers, ref.watch, and ref.read. This chapter focuses on one testing skill: replacing a provider's dependency inside a test.
A provider override changes what a ProviderContainer returns for a specific provider. The override applies only to that container. This lets you test a provider without using a network client, database, clock, or other external dependency.
Why overrides isolate a test
Consider a userNameProvider that reads a repository provider:
final userNameProvider = FutureProvider<String>((ref) {
final repository = ref.watch(userRepositoryProvider);
return repository.fetchName();
});In production, userRepositoryProvider might create an HTTP repository. A unit test should not make a real HTTP request. Instead, the test creates a fake repository and overrides the provider.
The provider graph then looks like this:
The code under test remains unchanged. Only its input changes.
| Production dependency | Test replacement | Benefit |
|---|---|---|
| HTTP repository | In-memory fake | No network access |
| Database provider | Temporary fake database | Predictable data |
| Clock provider | Fixed clock | Stable time-based assertions |
| Authentication provider | Test user provider | Controlled permissions |
Create an override
ProviderContainer accepts an overrides list. For a normal Provider, use overrideWithValue when you already have the replacement object.
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
abstract class UserRepository {
Future<String> fetchName();
}
final userRepositoryProvider = Provider<UserRepository>(
(ref) => throw UnimplementedError('Provide a repository override'),
);
final userNameProvider = FutureProvider<String>(
(ref) => ref.watch(userRepositoryProvider).fetchName(),
);
class FakeUserRepository implements UserRepository {
FakeUserRepository(this.name);
final String name;
@override
Future<String> fetchName() async => name;
}
void main() {
test('uses the fake repository', () async {
final container = ProviderContainer(
overrides: [
userRepositoryProvider
.overrideWithValue(FakeUserRepository('Ada')),
],
);
addTearDown(container.dispose);
final name = await container.read(userNameProvider.future);
expect(name, 'Ada');
});
}The test reads .future because userNameProvider is a FutureProvider. Reading .future returns a Future<String> that completes with the provider's data or fails with its error.
The override is local to this container. Another test that creates a new container receives the default provider behavior.
Fakes and mocks
A fake is a small working implementation used in tests. The FakeUserRepository above always returns a chosen name. Fakes are useful when you want readable behavior and do not need to verify every method call.
A mock records calls and can verify interactions, such as whether fetchName was called once. Use a mock when the interaction itself is part of the behavior you are testing. Use a fake when the returned result matters more than the interaction.
A fake can also model failure:
class FailingUserRepository implements UserRepository {
@override
Future<String> fetchName() async {
throw Exception('offline');
}
}
void main() {
test('surfaces repository errors', () async {
final container = ProviderContainer(
overrides: [
userRepositoryProvider
.overrideWithValue(FailingUserRepository()),
],
);
addTearDown(container.dispose);
await expectLater(
container.read(userNameProvider.future),
throwsA(isA<Exception>()),
);
});
}For providers that expose AsyncValue, inspect the resulting value with container.read(provider) after the asynchronous work completes. Assert separately on AsyncData, AsyncError, and the contained data or error.
Reusing overrides in widget tests
The same override list works with ProviderScope:
await tester.pumpWidget(
ProviderScope(
overrides: [
userRepositoryProvider
.overrideWithValue(FakeUserRepository('Ada')),
],
child: const MyApp(),
),
);This keeps widget tests deterministic while exercising the real provider graph and UI together.
Exercise
Why should a test override the repository provider instead of changing userNameProvider itself?
Show answer
Overriding the repository preserves the real userNameProvider logic while replacing only the external dependency. The test therefore checks the production behavior with controlled input.