Utrzymuj testy deterministyczne dzięki jawnym kontenerom.
Większość scenariuszy testowych mapuje się bezpośrednio na API ProviderContainer: read, invalidate, refresh, listen i override y dla każdej instancji providera.
Test Strategy
Podziel testy na czyste testy kontenerowe i testy integracyjne widgetów.
Pure Dart
Używaj ProviderContainer bezpośrednio do asercji read/invalidate/refresh
Overrides
Nadpisuj dla pojedynczej instancji providera za pomocą overrideWith / overrideWithValue
Widget tests
Wstrzykuj zewnętrzny kontener i zwalniaj go jawnie
Tip
ProviderContainer.listen nie emituje wartości początkowej, chyba że podano fireImmediately: true.
Polecenie testowe i lista kontrolna
Uruchom cały zestaw i sprawdź, że w scenariuszach autoDispose nie ma żadnych oczekujących wycieków timerów.
Command
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.
Przykład: override per argument
Providery podobne do rodzin są nadpisywane dla każdej instancji utworzonej przez funkcję fabryczną.
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();
Następne kroki
API Reference
Otwórz szybkie sygnatury dla ProviderContainer, Ref, AsyncValue i API mutation.
Open API Reference