Search packages, repositories and app categories

Package detailsv0.1.9

riverpod_test

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

AndroidiOSWebmacOSWindowsLinux
30-day downloads
4.6K
Likes
28
Pub points
150

Install package

flutter pub add riverpod_test

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

riverpod_test is a testing library designed to simplify the testing of Riverpod providers in Flutter applications. It provides utilities to easily override, mock, and test provider values in isolation, ensuring reliable and maintainable tests. Built specifically for the Riverpod package, it integrates seamlessly with Flutter's testing ecosystem, enabling developers to verify state logic without relying on real dependencies. The library supports all major platforms, making it ideal for cross-platform testing workflows.

Use cases

  • Testing provider state changes
  • Mocking dependencies in tests
  • Validating business logic in providers
  • Isolating unit tests from real data sources
  • Verifying async operations in providers

Key features

  • Easy provider overrides
  • Built-in mocking support
  • Integration with Flutter's test framework
  • Support for async and sync providers
  • Clean, readable test syntax

Suitable for

  • Flutter developers using Riverpod
  • Teams prioritizing test reliability
  • Projects requiring extensive provider testing
  • Developers writing unit and integration tests
  • Cross-platform Flutter applications

Considerations

  • Requires Riverpod package
  • Limited to Riverpod-specific testing
  • May need additional setup for complex scenarios
  • Not suitable for widget-level testing alone
  • Depends on external test environment configuration

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Pub Star on Github Flutter Website License: MIT Bloc Library


Package

Package is a port of felangel's bloc_test, modified to work with Riverpod Providers.
Increase code coverage by testing all riverpod providers.

Installation

For a Flutter project:

flutter pub add --dev riverpod_test

For a Dart project:

dart pub add --dev riverpod_test

Usage

testProvider

import 'package:riverpod_test.dart';

testProvider(
  'expect [0]',
  provider: counterProvider,
  expect: () => [0],
);

final counterProvider = Provider<int>((ref) => 0);

testNotifier

testNotifier(
  'expect [2] when increment is called twice and skip: 1',
  provider: counterNotifierProvider,
  act: (notifier) => notifier
      ..increment()
      ..increment(),
  skip: 1,
  expect: () => [2],
);

final counterNotifierProvider =
    NotifierProvider<CounterNotifier, int>(CounterNotifier.new);

class CounterNotifier extends Notifier<int> {
  @override
  int build() => 0;

  void increment() => state++;
}

testAsyncNotifier (also handles StreamNotifier)

testAsyncNotifier<CounterAsyncNotifier, int>(
  'expect [AsyncData(1)] when call increment',
  provider: counterAsyncNotifierProvider(0),
  act: (notifier) => notifier.increment(),
  expect: () => <AsyncValue<int>>[const AsyncData(1)],
);

testAsyncNotifier(
  'verify if mockRepository sideEffect is called',
  provider: sideEffectAsyncNotifierProvider(1),
  overrides: [repositoryProvider.overrideWithValue(mockRepository)],
  act: (notifier) => notifier.increment(),
  verify: (_) => verify(mockRepository.sideEffect).called(1),
);

final counterAsyncNotifierProvider =
    AsyncNotifierProviderFamily<CounterAsyncNotifier, int, int>(
  CounterAsyncNotifier.new,
);

class CounterAsyncNotifier extends FamilyAsyncNotifier<int, int> {
  @override
  FutureOr<int> build(int initialValue) => initialValue;

  void increment() => state = AsyncData(value + 1);
}

class MockRepository extends Mock implements Repository {}

final sideEffectAsyncNotifierProvider =
    AsyncNotifierProviderFamily<SideEffectAsyncNotifier, int, int>(
  SideEffectAsyncNotifier.new,
);

class SideEffectAsyncNotifier extends FamilyAsyncNotifier<int, int> {
  @override
  FutureOr<int> build(int initialValue) => initialValue;

  Repository get repository => ref.watch(repositoryProvider);

  void increment() {
    repository.sideEffect();
    state = AsyncData(value + 1);
  }
}

testResultProvider

testResultProvider<Repository>(
  'expect [1] when incrementCounter is called',
  provider: repositoryProvider,
  act: (result) => result.incrementCounter(),
  expect: () => [1],
);

final repositoryProvider = Provider<Repository>((ref) => Repository());

class Repository {
  int incrementCounter() => 1;
}

testStateNotifier

testStateNotifier<CounterStateNotifier, int>(
  'expect [0] when decrement is called and seed: 1',
  provider: counterStateNotifierProvider,
  act: (notifier) => notifier.decrement(),
  seed: 1,
  wait: const Duration(milliseconds: 100),
  expect: () => <int>[0],
);

final counterStateNotifierProvider =
    StateNotifierProvider<CounterStateNotifier, int>(
  (ref) => CounterStateNotifier(),
);

class CounterStateNotifier extends StateNotifier<int> {
  CounterStateNotifier() : super(0);

  void increment() => state++;

  Future<void> decrement() async {
    await Future<void>.delayed(const Duration(milliseconds: 100));
    state--;
  }
}

Categories

Dependencies

Dependency list 4 items

Source repository

Projects using this package

No data available yet.