Keep tests deterministic with explicit containers.

Most test scenarios map directly to ProviderContainer APIs: read, invalidate, refresh, listen, and overrides per provider instance.

Test Strategy

Split tests into pure container tests and widget integration tests.

Pure Dart

Use ProviderContainer directly for read/invalidate/refresh assertions

Overrides

Override per provider instance with overrideWith / overrideWithValue

Widget tests

Inject external container and dispose it explicitly

Совет

ProviderContainer.listen does not emit initial value unless fireImmediately: true is specified.

Test Command and Checklist

Run full suite and verify no pending timer leaks in 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.

Example: per-argument override

family-like providers are overridden per instance created from your factory function.

product_provider_test.dart
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();
This pattern mirrors the README and upstream tests in miniriverpod repository.
Widget tests should unmount widgets before disposing external containers.
Use fake_async to validate autoDispose delays and keepAlive behavior.

Следни чекори

StateProvider

Open quick signatures for ProviderContainer, Ref, AsyncValue, and mutation APIs.

Open API Reference

Back to Overview

Return to the full documentation map for miniriverpod.

Open Overview