bloc_presentation_test 0.3.0 bloc_presentation_test: ^0.3.0 copied to clipboard
A testing library for Blocs/Cubits which mixin BlocPresentationMixin. To be used with bloc_presentation package.
bloc_presentation_test #
A package which makes testing BlocPresentationMixin
ed Bloc
s/Cubit
s more straightforward.
To be used with bloc_presentation
package.
Installation #
flutter pub add --dev bloc_presentation_test
Usage #
There are 2 ways of stubbing presentation
stream:
- using
whenListenPresentation
function - extending target
BlocPresentationMixin
edBloc
/Cubit
withMockPresentationBloc
/MockPresentationCubit
1. Approach - whenListenPresentation
#
First, create a mock class of your BlocPresentationMixin
ed Bloc
/Cubit
.
For example, you can use bloc_test
package to achieve that.
class MockCommentCubit extends MockCubit implements CommentCubit {}
Then, create an instance of MockCommentCubit
and obtain StreamController
by calling whenListenPresentation
with
a newly created mocked cubit.
final mockCubit = MockCommentCubit();
final controller = whenListenPresentation(mockCubit);
It will stub MockCommentCubit
's presentation
stream, so you are able to subscribe to this stream.
Obtained controller can be used for adding events to presentation
stream.
The returned StreamController
is disposed in Cubit
's/Bloc
's close
method.
If you override the stub for this method then you need to dispose the controller manually.
controller.add(const FailedToUpvote());
If you specify initialEvents
argument, presentation
stream will be stubbed with a stream of given events.
final controller = whenListenPresentation(
mockCubit,
const [FailedToUpvote(), SuccessfulUpvote()],
);
2. Approach - MockPresentationCubit
/MockPresentationBloc
#
First, create a mock class of your BlocPresentationMixin
ed Bloc
/Cubit
using MockPresentationBloc
/MockPresentationCubit
.
class MockCommentCubit extends MockPresentationCubit<CommentState> implements CommentCubit {}
Then, create an instance of a MockCommentCubit
and call emitMockPresentation
.
final mockCubit = MockCommentCubit();
mockCubit.emitMockPresentation(const FailedToUpvote());
It will add FailedToUpvoteEvent
to presentation
stream.
After all, remember to call MockCommentCubit
's close
method.