riverpod_testing_library 0.2.0

SDKdartflutter
Platformandroidioswindowslinuxmacosweb

A testing library which makes it easy to test providers. Built to be used with the riverpod package.

Riverpod Testing Library

style: very good analysis License: MIT Coverage

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.2.0

Install it:

dart pub get

Supported providers

TypeType
Provider
(Async)NotifierProvider
StateNotifierProvider
FutureProvider
StreamProvider
StateProvider
ChangeNotifierProvider

API

ArgumentTypeDefaultDescription
providerProviderListenable<State>The provider under test.
overridesList<Override><Override>[]A list of Overrides that stores the state of the providers and allows overriding the behavior of a specific provider
setUpFutureOr<void> Function()?Used to set up any dependencies prior to initializing the [provider] under test.
skipint0Can be used to skip any number of states.
fireImmediatelyboolfalseTell Riverpod to immediately call the listener with the current value. Has no effect when expect is null.
actFutureOr<void> Function(ProviderContainer container)?Will be invoked with the ProviderContainer and should be used to interact with any provider.
expectObject Function()?Asserts that the provider updates with the expected states (in order) after [act] is executed.
verifyFutureOr<void> Function(ProviderContainer container)?Invoked after [act] and can be used for additional verification/assertions.
tearDownFutureOr<void> Function()?Used to execute any code after the test has run.

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;
    }),
  ],
);