Sub topic 10
Replacing Dependencies in Tests
Assume you know Dart tests, ProviderScope, ConsumerWidget, and basic generated providers. This chapter focuses on replacing a provider's implementation during a test. The production code stays unchanged, but the test controls which dependency Riverpod creates.
Why override a provider?
A provider often connects application code to an external system:
- An HTTP API
- A database
- A file system
- A clock
- An authentication service
Tests should not depend on those systems. A test double is a fake or mock implementation used instead of the real dependency.
ProviderScope accepts an overrides list. Each override changes how a provider is built for that scope and its descendants. In a unit test, use ProviderContainer, which is the non-widget equivalent of ProviderScope.
Override a dependency in a unit test
Suppose the application declares a repository provider and a family that loads one user:
// lib/user_providers.dart
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'user_providers.g.dart';
class User {
const User({required this.id, required this.name});
final int id;
final String name;
}
abstract interface class UserRepository {
Future<User> fetchUser(int id);
}
@riverpod
UserRepository userRepository(Ref ref) => ApiUserRepository();
@riverpod
Future<User> user(Ref ref, int id) {
return ref.watch(userRepositoryProvider).fetchUser(id);
}
class ApiUserRepository implements UserRepository {
@override
Future<User> fetchUser(int id) async {
throw UnimplementedError();
}
}The test can replace only userRepositoryProvider. The generated userProvider still runs its normal logic, including its family argument:
// test/user_providers_test.dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:test/test.dart';
import 'package:my_app/user_providers.dart';
class FakeUserRepository implements UserRepository {
@override
Future<User> fetchUser(int id) async {
return User(id: id, name: 'Ada');
}
}
void main() {
test('loads a user from the fake repository', () async {
final container = ProviderContainer(
overrides: [
userRepositoryProvider.overrideWith(
(ref) => FakeUserRepository(),
),
],
);
addTearDown(container.dispose);
final result = await container.read(userProvider(42).future);
expect(result.id, 42);
expect(result.name, 'Ada');
});
}container.read(userProvider(42).future) waits for the Future<User> to finish and returns the resolved User. Reading userProvider(7) would use the same fake repository but represent a different family instance.
Always dispose a ProviderContainer. Disposal releases provider state and resources, and addTearDown ensures cleanup even when the test fails.
Override a provider's result directly
Sometimes testing the dependency is unnecessary. You can replace the provider under test with a fixed value:
test('uses a supplied user', () {
final container = ProviderContainer(
overrides: [
userProvider(42).overrideWith(
(ref) async => const User(id: 42, name: 'Grace'),
),
],
);
addTearDown(container.dispose);
expect(
container.read(userProvider(42).future),
completion(const User(id: 42, name: 'Grace')),
);
});Use a dependency override when you want to test the real provider logic. Use a direct result override when another provider is irrelevant to the test.
Widget tests use the same idea
For a widget test, place the override on the nearest ProviderScope:
await tester.pumpWidget(
ProviderScope(
overrides: [
userRepositoryProvider.overrideWith(
(ref) => FakeUserRepository(),
),
],
child: const UserScreen(userId: 42),
),
);Widgets below this scope see the fake repository. Widgets outside it, if any, keep the normal provider behavior.
Common testing mistakes
- Forgetting
addTearDown(container.dispose), which can leave state between tests. - Overriding
userProvider(42)while the widget readsuserProvider(7). - Creating a real repository inside the provider under test instead of reading
userRepositoryProvider. That bypasses Riverpod overrides. - Using a fake that returns data with the wrong shape or identifier.
- Reading
.futurefrom a synchronous provider..futureis for asynchronous providers such asFutureProvider.
Exercise
You want to test that userProvider(5) handles a repository failure. Which provider should you override?
Show answer
Override userRepositoryProvider with a fake whose fetchUser throws. This keeps the real userProvider logic under test while replacing the external dependency.