Պահպանեք tests-ը deterministic՝ հստակ containers-ով։

Test scenarios-ի մեծ մասը ուղղակի map է լինում դեպի ProviderContainer APIs՝ read, invalidate, refresh, listen և provider instance-ի override-ներ։

Test Strategy

Բաժանեք tests-ը pure container tests-ի և widget integration tests-ի։

Pure Dart

read/invalidate/refresh assertions-ի համար ուղղակի օգտագործեք ProviderContainer-ը

Overrides

Override արեք provider instance-ի մակարդակով overrideWith / overrideWithValue-ով

Widget tests

Inject արեք արտաքին container-ը և այն explicit dispose արեք

Tip

ProviderContainer.listen-ը սկզբնական value-ը չի emit անում, եթե fireImmediately: true նշված չէ։

Test command և checklist

Գործարկեք ամբողջ suite-ը և ստուգեք, որ autoDispose scenarios-ում սպասվող 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.

Օրինակ՝ override ըստ argument-ի

family-like providers-ը override են արվում factory function-ով ստեղծված յուրաքանչյուր instance-ի համար։

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-ը հետևում է README-ին և upstream tests-ին miniriverpod repository-ում։
Widget tests-ը պետք է unmount անեն widget-ները արտաքին containers-ը dispose անելուց առաջ։
Օգտագործեք fake_async՝ autoDispose delays-ն ու keepAlive behavior-ը ստուգելու համար։

Հաջորդ քայլեր

API Reference

Բացեք արագ signatures-ը ProviderContainer-ի, Ref-ի, AsyncValue-ի և mutation APIs-ի համար։

Open API Reference

Back to Overview

Վերադարձեք miniriverpod-ի ամբողջ documentation map-ին։

Open Overview