واضح containers کے ساتھ tests کو deterministic رکھیں۔
زیادہ تر test scenarios براہِ راست ProviderContainer APIs پر map ہوتے ہیں: read، invalidate، refresh، listen، اور provider instance کے حساب سے overrides۔
Test Strategy
tests کو pure container tests اور widget integration tests میں تقسیم کریں۔
Pure Dart
read/invalidate/refresh assertions کے لیے براہِ راست ProviderContainer استعمال کریں
Overrides
overrideWith / overrideWithValue کے ساتھ provider instance کے حساب سے override کریں
Widget tests
external container inject کریں اور اسے صراحتاً dispose کریں
Tip
ProviderContainer.listen ابتدائی value emit نہیں کرتا جب تک fireImmediately: true نہ دیا جائے۔
Test command اور checklist
پورا suite چلائیں اور verify کریں کہ autoDispose scenarios میں کوئی pending timer leak نہیں ہے۔
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.
مثال: argument کے حساب سے override
family-like providers آپ کے factory function سے بننے والی ہر instance کے لیے override ہوتے ہیں۔
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();
aglay marahil
API Reference
ProviderContainer، Ref، AsyncValue، اور mutation APIs کے quick signatures کھولیں۔
Open API Reference