Keep tests deterministic with explicit containers.
Most test scenarios map directly to ProviderContainer APIs: read, invalidate, refresh, listen, and overrides per provider instance.
Test Strategy
Split tests into pure container tests and widget integration tests.
Pure Dart
Use ProviderContainer directly for read/invalidate/refresh assertions
Overrides
Override per provider instance with overrideWith / overrideWithValue
Widget tests
Inject external container and dispose it explicitly
Ábending
ProviderContainer.listen does not emit initial value unless fireImmediately: true is specified.
Test Command and Checklist
Run full suite and verify no pending timer leaks in autoDispose scenarios.
Command
flutter test
Checklist
- Dispose externally injected ProviderContainer in tearDown.
- Use fireImmediately: true when your listener assertions need initial state.
- For autoDispose tests, advance fake time beyond autoDisposeDelay.
Example: per-argument override
family-like providers are overridden per instance created from your factory function.
class ProductById extends Provider<Product> {
ProductById(this.id) : super.args((id,));
final String id;
@override
Product build(ref) {
final repo = ref.watch(productRepoProvider);
return repo.fetch(id);
}
}
final container = ProviderContainer(
overrides: [
productByIdProvider('a').overrideWithValue(const Product(id: 'a', name: 'stub')),
],
);
// assert and cleanup
container.dispose();
Næstu skref
StateProvider
Open quick signatures for ProviderContainer, Ref, AsyncValue, and mutation APIs.
Open API Reference