riverpod_test 0.1.3 copy "riverpod_test: ^0.1.3" to clipboard
riverpod_test: ^0.1.3 copied to clipboard

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

example/main.dart

import 'package:mocktail/mocktail.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_test/riverpod_test.dart';
import 'package:test/test.dart';

class MockRepository extends Mock implements Repository {}

void main() {
  mainProvider();
  mainNotifier();
  mainResultProvider();
  mainStateNotifier();
}

void mainProvider() {
  group('counterProvider', () {
    testProvider<int>(
      'expect [0]',
      provider: counterProvider,
      expect: () => const <int>[0],
    );
  });

  group('counterRepositoryProvider', () {
    final mockRepository = MockRepository();
    
    testProvider<int>(
      'expect [5] from repository',
      overrides: [repositoryProvider.overrideWithValue(mockRepository)],
      setUp: () => when(mockRepository.incrementCounter).thenReturn(5),
      provider: counterRepositoryProvider,
      expect: () => const <int>[5],
    );
  });
}

void mainNotifier() {
  group('counterNotifierProvider', () {
    testNotifier<CounterNotifier, int>(
      'expect [1] when increment is called',
      provider: counterNotifierProvider,
      act: (notifier) => notifier.increment(),
      expect: () => const <int>[1],
    );

    testAsyncNotifier(
      'expect [AsyncData(2)] when increment is called with seed: AsyncData(1)',
      provider: counterAsyncNotifierProvider,
      seed: const AsyncData(1),
      act: (notifier) => notifier.increment(),
      expect: () => [const AsyncData(2)],
    );
  });
}

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

void mainStateNotifier() {
  testStateNotifier(
    'expect [2] when increment is called twice',
    provider: counterStateNotifierProvider,
    act: (notifier) => notifier
      ..increment()
      ..increment(),
    expect: () => [2],
  );
}

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

final counterRepositoryProvider =
    Provider<int>((ref) => ref.watch(repositoryProvider).incrementCounter());

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

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

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

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

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

  void increment() => state++;
}

class CounterAsyncNotifier extends AsyncNotifier<int> {
  @override
  int build() => 0;

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

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

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

  void increment() => state++;
}
17
likes
0
pub points
80%
popularity

Publisher

verified publishereronsoft.com

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

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

diff_match_patch, meta, riverpod, test

More

Packages that depend on riverpod_test