Sub topic 15
Testing providers with ProviderContainer and overrides
Assume you know Dart classes, generics, Future, async and await, Flutter widgets, and the basics of Riverpod providers and notifiers. This chapter focuses on testing provider logic without building a widget tree.
Use ProviderContainer for provider tests
A ProviderContainer is the runtime that stores provider state. ProviderScope creates this runtime for a Flutter application. In a unit test, create the container directly:
final container = ProviderContainer();
addTearDown(container.dispose);Always dispose the container. Disposal releases provider state and resources after the test.
Use container.read to read a provider:
expect(container.read(counterProvider), 0);
container.read(counterProvider.notifier).increment();
expect(container.read(counterProvider), 1);The first read initializes the provider if needed. Reading .notifier gives access to the object that exposes state-changing methods.
Replace dependencies with overrides
A provider often depends on a repository that talks to a database or web service. A unit test should not need that real service. Define the dependency as a provider, then replace it in the test.
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
class Todo {
const Todo(this.id, this.title);
final int id;
final String title;
}
abstract class TodoRepository {
Future<List<Todo>> fetchAll();
}
final todoRepositoryProvider = Provider<TodoRepository>(
(ref) => throw UnimplementedError(),
);
final todoListProvider = FutureProvider<List<Todo>>((ref) {
return ref.watch(todoRepositoryProvider).fetchAll();
});
class FakeTodoRepository implements TodoRepository {
@override
Future<List<Todo>> fetchAll() async {
return [const Todo(1, 'Write tests')];
}
}
void main() {
test('loads todos from the replaced repository', () async {
final container = ProviderContainer(
overrides: [
todoRepositoryProvider.overrideWithValue(
FakeTodoRepository(),
),
],
);
addTearDown(container.dispose);
final todos = await container.read(todoListProvider.future);
expect(todos.single.title, 'Write tests');
});
}The production repository provider throws because the application must supply a real implementation. The test supplies a fake implementation instead. The todoListProvider does not need to change because it still reads the same dependency provider.
Testing asynchronous states
For a FutureProvider or AsyncNotifierProvider, read .future when the test needs the successful value:
final todos = await container.read(todoListProvider.future);This waits for the provider to finish. To inspect loading or error behavior, listen to the provider and collect its AsyncValue states, or read the provider after the test has completed. Test errors explicitly rather than assuming every request succeeds.
The test boundary
The following flow keeps external services outside the unit test:
Use overrides for clocks, repositories, configuration, and authentication. This makes tests deterministic and avoids network access.
Exercise
Why should a provider test call container.dispose()?
Show answer
Disposal releases provider state and resources, preventing state from leaking into other tests.