BlocTester<TBloc extends JuiceBloc<TState>, TState extends BlocState> class

A test utility class for testing Juice blocs.

BlocTester simplifies bloc testing by providing:

  • Automatic stream emission tracking
  • Convenient assertion methods
  • Proper cleanup handling

Example

test('increments counter', () async {
  final bloc = CounterBloc();
  final tester = BlocTester(bloc);

  await tester.send(IncrementEvent());

  tester.expectState((state) => state.count == 1);
  tester.expectLastStatusIs<UpdatingStatus>();

  await tester.dispose();
});

Constructors

BlocTester(TBloc bloc)
Creates a BlocTester for the given bloc.

Properties

bloc → TBloc
The bloc being tested.
final
emissions List<StreamStatus<TState>>
All stream emissions captured during the test.
no setter
hashCode int
The hash code for this object.
no setterinherited
lastState → TState
The last emitted state, or the current bloc state if no emissions.
no setter
lastStatus StreamStatus<TState>?
The last emitted status, or null if none.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
state → TState
The current state of the bloc.
no setter

Methods

clearEmissions() → void
Clears all recorded emissions.
dispose() Future<void>
Disposes of the tester and closes the bloc.
expectAllEmissions(bool predicate(StreamStatus<TState>), [String? reason]) → void
Asserts that all emissions match the predicate.
expectAnyEmission(bool predicate(StreamStatus<TState>), [String? reason]) → void
Asserts that at least one emission matches the predicate.
expectEmissionCount(int count, [String? reason]) → void
Asserts the number of emissions.
expectLastStatusIs<T extends StreamStatus<BlocState>>([String? reason]) → void
Asserts that the last emitted status is of the given type.
expectNoFailure([String? reason]) → void
Asserts that no failure status was emitted.
expectState(bool predicate(TState state), [String? reason]) → void
Asserts that the current state matches the predicate.
expectStateEquals(TState expected) → void
Asserts that the current state equals the expected state.
expectStatusSequence(List<Type> expectedTypes, {int? skip}) → void
Asserts that the emissions match the expected status type sequence.
expectWasFailure([String? reason]) → void
Asserts that a failure status was emitted.
expectWasWaiting([String? reason]) → void
Asserts that a waiting status was emitted.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
send(EventBase event, {Duration? delay}) Future<void>
Sends an event to the bloc and waits for processing.
sendAndWaitForResult(EventBase event, {Duration timeout = const Duration(seconds: 5)}) Future<StreamStatus<TState>>
Sends an event and waits until a non-waiting status is emitted.
toString() String
A string representation of this object.
inherited
waitForEmissions(int count, {Duration? timeout}) Future<void>
Waits for a specific number of emissions.

Operators

operator ==(Object other) bool
The equality operator.
inherited