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();
Where to go next
miniriverpod overview
Find the setup guide and the rest of the miniriverpod docs.
Back to the overview