Tests
Most test scenarios map directly ? ProviderContainer APIs: read, invalidate, refresh, listen, et overrides per provider instance.
Test Strategy
Split tests into pure container tests et widget integration tests.
Pure Dart
Use ProviderContainer directly pour read/invalidate/refresh assertions
Overrides
Override per provider instance avec overrideWith / overrideWithValue
Widget tests
Inject external container et dispose it explicitly
Tip
ProviderContainer.listen does not emit initial value unless fireImmediately: true is specified.
Test Command et Checklist
Run full suite et verify no pending timer leaks dans 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.
exemple: per-argument override
family-like providers are overridden per instance created de 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();
?tapes suivantes
API R?f?rence
Open quick signatures pour ProviderContainer, Ref, AsyncValue, et mutation APIs.
Open API R?f?rence