riverpod_testing_library 0.1.0
riverpod_testing_library: ^0.1.0 copied to clipboard
A testing library which makes it easy to test providers. Built to be used with the riverpod state management package.
Riverpod Testing Library #
A testing library which makes it easy to test providers. Built to be used with the riverpod state management package. Inspired by bloc_test.
Getting started #
Add riverpod_testing_library to your pubspec.yaml:
dev_dependencies:
riverpod_testing_library: 0.1.0
Install it:
dart pub get
Usage examples #
Write unit tests with providerTest #
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;
}),
],
);