providerTest<T> function

  1. @isTest
void providerTest<T>(
  1. String description, {
  2. required ProviderListenable<T> provider,
  3. List<Override> overrides = const <Override>[],
  4. FutureOr<void> setUp()?,
  5. int skip = 0,
  6. bool fireImmediately = false,
  7. FutureOr<void> act(
    1. ProviderContainer container
    )?,
  8. Object expect()?,
  9. FutureOr<void> verify(
    1. ProviderContainer container
    )?,
  10. FutureOr<void> tearDown()?,
})

Creates a new riverpod-specific test case with the given description. providerTest will handle asserting that the provider updates with the expected states (in order) after act is executed.

provider should be the provider under test.

overrides is a list of Overrides that stores the state of the providers and allows overriding the behavior of a specific provider.

setUp is optional and should be used to set up any dependencies prior to initializing the provider under test. setUp should be used to set up state necessary for a particular test case. For common set up code, prefer to use setUp from package:test/test.dart.

skip is an optional int which can be used to skip any number of states. skip defaults to 0.

fireImmediately (false by default) can be optionally passed to tell Riverpod to immediately call the listener with the current value. Has no effect when expect is null.

act is an optional callback which will be invoked with the ProviderContainer and should be used to interact with any provider.

expect is an optional Function that returns a Matcher that asserts that the provider updates with the expected states (in order) after act is executed.

verify is an optional callback which is invoked after act and can be used for additional verification/assertions.

tearDown is optional and can be used to execute any code after the test has run. tearDown should be used to clean up after a particular test case. For common tear down code, prefer to use tearDown from package:test/test.dart.

Implementation

@isTest
void providerTest<T>(
  String description, {
  required ProviderListenable<T> provider,
  List<Override> overrides = const <Override>[],
  FutureOr<void> Function()? setUp,
  int skip = 0,
  bool fireImmediately = false,
  FutureOr<void> Function(ProviderContainer container)? act,
  Object Function()? expect,
  FutureOr<void> Function(ProviderContainer container)? verify,
  FutureOr<void> Function()? tearDown,
}) {
  // ignore: avoid-passing-async-when-sync-expected
  test.test(description, () async {
    await testProvider(
      provider: provider,
      providerOverrides: overrides,
      setUp: setUp,
      skip: skip,
      fireImmediately: fireImmediately,
      act: act,
      expect: expect,
      verify: verify,
      tearDown: tearDown,
    );
  });
}