bloc_test 7.1.0
bloc_test: ^7.1.0 copied to clipboard
A testing library which makes it easy to test blocs. Built to be used with the bloc state management package.
A Dart package that makes testing blocs and cubits easy. Built to work with bloc and mockito.
Learn more at bloclibrary.dev!
Create a Mock #
import 'package:bloc_test/bloc_test.dart';
class MockCounterBloc extends MockBloc<int> implements CounterBloc {}
class MockCounterCubit extends MockBloc<int> implements CounterCubit {}
Stub the State Stream #
whenListen creates a stub response for the listen
method on a bloc or cubit. Use whenListen
if you want to return a canned Stream
of states. whenListen
also handles stubbing the state
to stay in sync with the emitted state.
// Create a mock instance
final counterBloc = MockCounterBloc();
// Stub the state stream
whenListen(counterBloc, Stream.fromIterable([0, 1, 2, 3]));
// Assert that the stubbed stream is emitted.
await expectLater(counterBloc, emitsInOrder(<int>[0, 1, 2, 3])))
// Assert that the current state is in sync with the stubbed stream.
expect(counterBloc.state, equals(3));
Unit Test with blocTest #
blocTest creates a new cubit
-specific test case with the given description
. blocTest
will handle asserting that the cubit
emits the expect
ed states (in order) after act
is executed. blocTest
also handles ensuring that no additional states are emitted by closing the cubit
stream before evaluating the expect
ation.
build
should be used for all cubit
initialization and preparation and must return the cubit
under test.
seed
is an optional state which will be used to seed the cubit
before act
is called.
act
is an optional callback which will be invoked with the cubit
under test and should be used to add
events to the cubit
.
skip
is an optional int
which can be used to skip any number of states and defaults to 0
.
wait
is an optional Duration
which can be used to wait for async operations within the cubit
under test such as debounceTime
.
expect
is an optional Iterable<State>
which the cubit
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 cubit
returned by build
.
errors
is an optional Iterable
of error matchers which the cubit
under test is expected to have thrown after act
is executed.
group('CounterBloc', () {
blocTest(
'emits [] when nothing is added',
build: () => CounterBloc(),
expect: [],
);
blocTest(
'emits [1] when CounterEvent.increment is added',
build: () => CounterBloc(),
act: (bloc) => bloc.add(CounterEvent.increment),
expect: [1],
);
});
blocTest
can optionally be used with a seeded state.
blocTest(
'CounterCubit emits [10] when seeded with 9',
build: () => CounterCubit(),
seed: 9,
act: (cubit) => cubit.increment(),
expect: [10],
);
blocTest
can also be used to skip
any number of emitted states before asserting against the expected states. The default value is 0.
blocTest(
'CounterBloc emits [2] when CounterEvent.increment is added twice',
build: () => CounterBloc(),
act: (bloc) => bloc..add(CounterEvent.increment)..add(CounterEvent.increment),
skip: 1,
expect: [2],
);
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: () => 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: () => CounterBloc(),
act: (bloc) => bloc.add(CounterEvent.increment),
expect: [1],
verify: (_) {
verify(repository.someMethod(any)).called(1);
}
);
blocTest
can also be used to expect that exceptions have been thrown.
blocTest(
'CounterBloc throws Exception when null is added',
build: () => CounterBloc(),
act: (bloc) => bloc.add(null),
errors: [
isA<Exception>(),
]
);
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: () => MyBloc(),
act: (bloc) => bloc.add(MyEvent()),
expect: [isA<StateB>()],
);
Dart Versions #
- Dart 2: >= 2.7.0