Set up the container so tests are easy to repeat

Use ProviderContainer directly to test read, invalidate, refresh, and listen. Override individual instances to supply values for the case you want to test.

What to test and where

Test the container on its own, then use widget tests to check how the UI connects to it.

Unit tests in Dart

Call read, invalidate, and refresh directly on ProviderContainer.

Provider overrides

Call overrideWith or overrideWithValue for the instance you want to replace.

Widget tests

Dispose of any container you pass into the scope.

Check the starting value

Set fireImmediately: true on ProviderContainer.listen when the callback should receive the current value first.

Run the tests and check cleanup

Run the full suite and check that autoDispose tests leave no pending timers.

Run the tests

flutter test

Checks before you finish

- Dispose of any ProviderContainer you supplied in tearDown.
- Set fireImmediately: true when a listener test needs the current value first.
- Advance fake time past autoDisposeDelay when you test autoDispose.

Override a provider for specific arguments

With a family-like provider, override the instance returned by the factory for the args you're testing.

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 approach follows the miniriverpod README and the project's own tests.
In a widget test, unmount the widget before disposing of a container you passed in.
Use fake_async to test the autoDispose delay and what happens with keepAlive.

Where to go next

API Reference

Look up the ProviderContainer, Ref, AsyncValue, and mutation APIs.

Look up an API

miniriverpod overview

Find the setup guide and the rest of the miniriverpod docs.

Back to the overview