riverpod-testing
Riverpod — Testing
Instructions
Riverpod is designed for testability: isolate state per test, mock via overrides, and keep the test environment close to production.
Unit tests (no Flutter)
Use ProviderContainer.test() to create a container for the test. Do not share containers between tests.
void main() {
test('Some description', () {
final container = ProviderContainer.test();
expect(container.read(provider), equals('some value'));
});
}
- container.read(provider) — Read current value.
- container.listen(provider, (prev, next) {}) — Listen and get a subscription; use subscription.read() to read. Prefer listen when the provider is auto-dispose so it is not disposed mid-test.
final subscription = container.listen<String>(provider, (_, _) {});
expect(subscription.read(), 'Some value');
Widget tests
Wrap the widget under test in ProviderScope:
testWidgets('Some description', (tester) async {
await tester.pumpWidget(
const ProviderScope(child: YourWidgetYouWantToTest()),
);
});
To interact with providers in the test, get the container with tester.container():
final container = tester.container();
expect(container.read(provider), 'some value');
Mocking providers
Use overrides on ProviderContainer or ProviderScope. All providers can be overridden without extra setup.
final container = ProviderContainer.test(
overrides: [
exampleProvider.overrideWith((ref) => 'Hello from tests'),
],
);
// Or in widget tests:
await tester.pumpWidget(
ProviderScope(
overrides: [exampleProvider.overrideWith((ref) => 'Hello from tests')],
child: const YourWidgetYouWantToTest(),
),
);
See riverpod-overrides for family overrides and other override methods.
Awaiting async providers
Read provider.future to get a Future that completes with the provider value; use with expectLater:
await expectLater(
container.read(provider.future),
completion('some value'),
);
Listening / spying
Use container.listen(provider, callback) and assert on the callback arguments or collect values in a list for assertions. Works with mockito/mocktail verify patterns.
Mocking Notifiers
Prefer mocking a dependency (e.g. repository) the Notifier uses rather than mocking the Notifier. If you must mock a Notifier, subclass it (do not implement), so the mock extends the original base class. With code generation, the mock usually needs to live in the same file as the Notifier to access the generated base class.
See riverpod-overrides and the official docs for more examples.
More from serverpod/skills-registry
riverpod-codegen-and-hooks
Use Riverpod code generation (@riverpod, riverpod_generator) and hooks (hooks_riverpod, HookConsumerWidget, flutter_hooks with Riverpod). Use when the user asks about @riverpod, code generation, riverpod_generator, when to use codegen, or using flutter_hooks with Riverpod (HookConsumerWidget, HookConsumer).
25riverpod-providers
Declare and use Riverpod providers (Provider, FutureProvider, StreamProvider, NotifierProvider, AsyncNotifierProvider, StreamNotifierProvider); unmodifiable vs modifiable, top-level declaration, Ref, Notifier build method. Use when creating providers, choosing provider type, writing Notifier classes, or understanding Riverpod state. Use this skill whenever the user asks about Riverpod providers, provider types, or notifiers.
24riverpod-consumers
Use Riverpod Consumer, ConsumerWidget, and ConsumerStatefulWidget to read and watch providers in widgets; WidgetRef, builder ref parameter. Use when building widgets that need to access Riverpod providers, ref.watch or ref.read in the UI, or converting StatelessWidget to ConsumerWidget. Prefer this skill when the user asks how to use providers in Flutter widgets or why ConsumerWidget is required.
19riverpod-overrides
Override Riverpod providers for tests, debugging, or environment-specific behavior; ProviderScope and ProviderContainer overrides, overrideWith, overrideWithValue. Use when mocking providers in tests, injecting different implementations, or scoping provider behavior. Use this skill when the user asks about overrides, testing Riverpod, or mocking providers.
16riverpod-migration
Migrate Riverpod from StateNotifier to Notifier/AsyncNotifier, from ChangeNotifier to AsyncNotifier, or upgrade from 0.13/0.14/1.0; ref.onDispose, family, lifecycle, riverpod migrate CLI. Use when the user asks about migrating from StateNotifier, from ChangeNotifier, upgrading Riverpod 0.13 to 0.14, 0.14 to 1.0, or Riverpod migration guides.
15riverpod-cancel
Cancel or debounce Riverpod async requests with ref.onDispose; cancel when user leaves the page, debounce rapid refreshes. Use when the user asks about cancelling requests, debouncing, or cleaning up when a provider is disposed.
15