ਸਪਸ਼ਟ 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();
ਇਹ pattern miniriverpod repository ਵਿੱਚ README ਅਤੇ upstream tests ਦੀ ਪਾਲਣਾ ਕਰਦਾ ਹੈ।
widget tests ਨੂੰ external containers dispose ਕਰਨ ਤੋਂ ਪਹਿਲਾਂ widgets ਨੂੰ unmount ਕਰਨਾ ਚਾਹੀਦਾ ਹੈ।
autoDispose delays ਅਤੇ keepAlive behavior ਦੀ ਪੁਸ਼ਟੀ ਲਈ fake_async ਵਰਤੋ।

ਅਗਲੇ ਕਦਮ

API Reference

ProviderContainer, Ref, AsyncValue, ਅਤੇ mutation APIs ਦੇ quick signatures ਖੋਲ੍ਹੋ।

Open API Reference

Back to Overview

miniriverpod ਦੇ ਪੂਰੇ documentation map ਤੇ ਵਾਪਸ ਜਾਓ।

Open Overview