bloc_test 4.0.0 copy "bloc_test: ^4.0.0" to clipboard
bloc_test: ^4.0.0 copied to clipboard

outdated

A testing library which makes it easy to test blocs. Built to be used with the bloc state management package.

Bloc Test Package

Pub build codecov style: effective dart Flutter Website Awesome Flutter Flutter Samples Star on GitHub Discord License: MIT


A Dart package that makes testing blocs easy. Built to work with bloc and mockito.

Create a Mock Bloc #

import 'package:bloc_test/bloc_test.dart';

class MockCounterBloc extends MockBloc<CounterEvent, int> implements CounterBloc {}

Stub the Bloc Stream #

whenListen creates a stub response for the listen method on a Bloc. Use whenListen if you want to return a canned Stream of states for a bloc instance.

// Create a mock instance
final counterBloc = MockCounterBloc();

// Stub the bloc `Stream`
whenListen(counterBloc, Stream.fromIterable([0, 1, 2, 3]));

// Assert that the bloc emits the stubbed `Stream`.
expectLater(counterBloc, emitsInOrder(<int>[0, 1, 2, 3])))

Unit Test a Real Bloc with blocTest #

blocTest creates a new bloc-specific test case with the given description. blocTest will handle asserting that the bloc emits the expected states (in order) after act is executed. blocTest also handles ensuring that no additional states are emitted by closing the bloc stream before evaluating the expectation.

build should be used for all bloc initialization and preparation and must return the bloc under test as a Future.

act is an optional callback which will be invoked with the bloc under test and should be used to add events to the bloc.

skip is an optional int which can be used to skip any number of states. The default value is 1 which skips the initialState of the bloc. skip can be overridden to include the initialState by setting skip to 0.

wait is an optional Duration which can be used to wait for async operations within the bloc under test such as debounceTime.

expect is an Iterable<State> which the bloc under test is expected to emit after act is executed.

verify is an optional callback which is invoked after expect and can be used for additional verification/assertions. verify is called with the bloc returned by build.

group('CounterBloc', () {
  blocTest(
    'emits [] when nothing is added',
    build: () async => CounterBloc(),
    expect: [],
  );

  blocTest(
    'emits [1] when CounterEvent.increment is added',
    build: () async => CounterBloc(),
    act: (bloc) => bloc.add(CounterEvent.increment),
    expect: [1],
  );
});

blocTest can also be used to skip any number of emitted states before asserting against the expected states. The default value is 1 which skips the initialState of the bloc. skip can be overridden to include the initialState by setting skip to 0.

blocTest(
  'CounterBloc emits [0, 1] when CounterEvent.increment is added',
  build: () async => CounterBloc(),
  act: (bloc) => bloc.add(CounterEvent.increment),
  skip: 0,
  expect: [0, 1],
);

blocTest can also be used to wait for async operations like debounceTime by providing a Duration to wait.

blocTest(
  'CounterBloc emits [1] when CounterEvent.increment is added',
  build: () async => CounterBloc(),
  act: (bloc) => bloc.add(CounterEvent.increment),
  wait: const Duration(milliseconds: 300),
  expect: [1],
);

blocTest can also be used to verify internal bloc functionality.

blocTest(
  'CounterBloc emits [1] when CounterEvent.increment is added',
  build: () async => CounterBloc(),
  act: (bloc) => bloc.add(CounterEvent.increment),
  expect: [1],
  verify: () async {
    verify(repository.someMethod(any)).called(1);
  }
);

Note: when using blocTest with state classes which don't override == and hashCode you can provide an Iterable of matchers instead of explicit state instances.

blocTest(
  'emits [StateB] when MyEvent is added',
  build: () async => MyBloc(),
  act: (bloc) => bloc.add(MyEvent()),
  expect: [isA<StateB>()],
);

Assert with Confidence using emitsExactly #

emitsExactly is similar to emitsInOrder but asserts that the provided bloc emits only the expected states in the exact order in which they were provided.

group('CounterBloc', () {
  test('emits [] when nothing is added', () async {
    final bloc = CounterBloc();
    await emitsExactly(bloc, []);
  });

  test('emits [1] when CounterEvent.increment is added', () async {
    final bloc = CounterBloc();
    bloc.add(CounterEvent.increment);
    await emitsExactly(bloc, [1]);
  });
});

emitsExactly also supports Matchers for states which don't override == and hashCode.

test('emits [StateB] when EventB is added', () async {
  final bloc = MyBloc();
  bloc.add(EventB());
  await emitsExactly(bloc, [isA<StateB>()]);
});

skip is an optional int which defaults to 1 and can be used to skip any number of states. The default behavior skips the initialState of the bloc but can be overridden to include the initialState by setting skip to 0.

test('emits [0, 1] when CounterEvent.increment is added', () async {
  final bloc = CounterBloc();
  bloc.add(CounterEvent.increment);
  await emitsExactly(bloc, [0, 1], skip: 0);
});

emitsExactly also takes an optional duration which is useful in cases where the bloc is using debounceTime or other similar operators.

test('emits [1] when CounterEvent.increment is added', () async {
  final bloc = CounterBloc();
  bloc.add(CounterEvent.increment);
  await emitsExactly(bloc, [1], duration: const Duration(milliseconds: 300));
});

Dart Versions #

  • Dart 2: >= 2.6.0

Maintainers #

551
likes
0
pub points
98%
popularity

Publisher

verified publisherbloclibrary.dev

A testing library which makes it easy to test blocs. Built to be used with the bloc state management package.

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

bloc, meta, mockito, test

More

Packages that depend on bloc_test