bloc_presentation_test 1.0.2 copy "bloc_presentation_test: ^1.0.2" to clipboard
bloc_presentation_test: ^1.0.2 copied to clipboard

A testing library for Blocs/Cubits which mixin BlocPresentationMixin. To be used with bloc_presentation package.

bloc_presentation_test #

pub.dev badge Build status

A package which makes testing BlocPresentationMixined Blocs/Cubits 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:

  1. using whenListenPresentation function
  2. extending target BlocPresentationMixined Bloc/Cubit with MockPresentationBloc/MockPresentationCubit

1. Approach - whenListenPresentation #

First, create a mock class of your BlocPresentationMixined 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 BlocPresentationMixined 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.

10
likes
140
pub points
49%
popularity

Publisher

verified publisherleancode.co

A testing library for Blocs/Cubits which mixin BlocPresentationMixin. To be used with bloc_presentation package.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (LICENSE)

Dependencies

bloc, bloc_presentation, diff_match_patch, flutter, meta, mocktail, test

More

Packages that depend on bloc_presentation_test