Skip to content
pubscout

bloc_test

10.0.0, released 2 years ago by bloclibrary.dev

A testing library that makes it easy to test blocs and cubits.

WASM-readyQuiet but busy
$ flutter pub add bloc_test
Source repository

What it does

Provides utilities like blocTest and whenListen to simplify testing state changes, emitted states, and async operations in blocs and cubits. It handles stream closing and expectations sequencing automatically.

AI-written summary of version 10.0.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You are writing unit tests for blocs or cubits
  • You need to verify emitted states in order after an action
  • You need to stub state streams using whenListen

Skip it when

  • You are not using the bloc state management package
  • You are testing UI widgets rather than business logic

Strengths

  • Simplifies asynchronous bloc testing with clear syntax
  • Provides built-in support for seeding states, skipping events, and waiting on async operations
  • Integrates seamlessly with package:test and mocktail

Weaknesses

  • Strictly tied to the bloc ecosystem

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
5

not documented

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

Alternatives

Packages that do the same job and could replace bloc_test.

  • riverpod

    Choose if you use Riverpod for state management instead of bloc.

Works with

Packages commonly used together with bloc_test: ones it builds on, ones built on it, and its own family.