Sub topic 11
Testing Cubits and Blocs with `bloc_test`
This chapter assumes you know Dart classes, async/await, Flutter testing basics, and how Cubits and Blocs expose state. The examples use the bloc, flutter_bloc, bloc_test, and flutter_test packages.
Tests for state management should verify behavior:
- Start with a known state.
- Perform an action, such as calling a Cubit method or adding a Bloc event.
- Assert the exact states emitted.
- Test failures and asynchronous work separately.
bloc_test provides the blocTest function for this pattern. It builds the state object, runs an action, and compares the emitted states with your expectation.
| Component | Action under test | Main assertion |
|---|---|---|
| Cubit | Call a method | Expected state sequence |
| Bloc | Add an event | Expected state sequence |
| Async state holder | Complete or fail a dependency | Loading, success, or failure states |
Testing a Cubit
This Cubit starts at zero and emits a new integer whenever increment runs:
import 'package:flutter_bloc/flutter_bloc.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
}The test names the behavior, creates the Cubit with build, performs the action with act, and checks the emitted values with expect:
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('CounterCubit', () {
blocTest<CounterCubit, int>(
'emits [1] when increment is called',
build: CounterCubit.new,
act: (cubit) => cubit.increment(),
expect: () => [1],
);
});
}The initial state, 0, is not included in expect. The assertion contains only states emitted after the action. If increment emits twice, write expect: () => [1, 2].
You can check the starting state with seed when a test needs to begin from a non-default value:
blocTest<CounterCubit, int>(
'increments an existing count',
build: CounterCubit.new,
seed: () => 4,
act: (cubit) => cubit.increment(),
expect: () => [5],
);Testing an event-driven Bloc
A Bloc receives events instead of direct method calls:
import 'package:flutter_bloc/flutter_bloc.dart';
sealed class CounterEvent {}
final class CounterIncremented extends CounterEvent {}
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<CounterIncremented>((event, emit) {
emit(state + 1);
});
}
}Test it by adding the event:
blocTest<CounterBloc, int>(
'emits [1] when incremented',
build: CounterBloc.new,
act: (bloc) => bloc.add(CounterIncremented()),
expect: () => [1],
);Use wait when the behavior includes a deliberate asynchronous delay, such as debouncing. Do not add arbitrary delays to ordinary synchronous tests.
Testing errors and dependencies
Inject dependencies instead of constructing real network clients inside a Cubit. A fake repository can return known data or throw a known exception. Then write separate tests for loading and success, and for loading and failure. This keeps tests fast and makes each failure meaningful.
Compare state objects by value. If a state contains lists or fields, use Equatable or implement equality so bloc_test can identify the expected result reliably.
Exercise
A Cubit starts at false and its toggle method emits !state. What should the expect callback contain after calling toggle twice?
Show answer
expect: () => [true, false]