A testing library which makes it easy to test providers. Built to be used with the riverpod state management package. Inspired by bloc_test.
Add riverpod_testing_library
to your pubspec.yaml
:
dev_dependencies:
riverpod_testing_library: 0.2.0
Install it:
dart pub get
Type | Type |
---|---|
Provider | ✅ |
(Async)NotifierProvider | ✅ |
StateNotifierProvider | ✅ |
FutureProvider | ✅ |
StreamProvider | ✅ |
StateProvider | ✅ |
ChangeNotifierProvider | ✅ |
Argument | Type | Default | Description |
---|---|---|---|
provider |
ProviderListenable<State> |
The provider under test. | |
overrides |
List<Override> |
<Override>[] |
A list of Overrides that stores the state of the providers and allows overriding the behavior of a specific provider |
setUp |
FutureOr<void> Function()? |
Used to set up any dependencies prior to initializing the [provider] under test. | |
skip |
int |
0 |
Can be used to skip any number of states. |
fireImmediately |
bool |
false |
Tell Riverpod to immediately call the listener with the current value. Has no effect when expect is null. |
act |
FutureOr<void> Function(ProviderContainer container)? |
Will be invoked with the ProviderContainer and should be used to interact with any provider. | |
expect |
Object Function()? |
Asserts that the provider updates with the expected states (in order) after [act] is executed. |
|
verify |
FutureOr<void> Function(ProviderContainer container)? |
Invoked after [act] and can be used for additional verification/assertions. | |
tearDown |
FutureOr<void> Function()? |
Used to execute any code after the test has run. |
providerTest
creates a new provider-specific tests case. It will handle asserting that the provider updates with the expected states (in order). It also handles ensuring that no additional states are stored by disposing the container being used in a test.
group('counterProvider', () {
providerTest<int>(
'emits the initial state when fireImmediately is true',
provider: counterProvider,
fireImmediately: true,
expect: () => [0],
);
providerTest<int>(
'emits [] when nothing is done',
provider: counterProvider,
expect: () => [],
);
providerTest<int>(
'emits [1] when Counter.increment() is called',
provider: counterProvider,
act: (container) => container.read(counterProvider.notifier).increment(),
expect: () => [1],
);
});
When using providerTest
with state classes which don't override ==
and hashCode
you can provide an Iterable
of matchers instead of explicit state instances.
providerTest<int>(
'emits [1] when Counter.increment() is called',
provider: counterProvider,
act: (container) => container.read(counterProvider.notifier).increment(),
expect: () => [
predicate<int>((value) {
expect(value, 1);
return true;
}),
],
);