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.

Libraries

bloc_presentation_test
A testing library for bloc_presentation.