-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support testing of emit.forEach
#4206
Comments
Hi @haf 👋 You can take a look at the Flutter Todos Example for a reference. If that doesn't help, then feel free to share a link to a minimal reproduction sample and I'm happy to look and open a PR with suggestions. |
So I'm asking as if: bloc/examples/flutter_todos/test/todos_overview/bloc/todos_overview_bloc_test.dart Lines 117 to 120 in 8bbd6c4
bloc.add inside a stream listening callback — which is what I'd like to test.
Let's start by understanding each other before either of us invests into building any examples or features :) I'd love to answer any questions and/or discuss as needed! |
Hope you've had a great weekend! Do you have any further questions that might be clarifying? |
Hey @haf, thanks! Hope you had a great weekend as well! Can you share a snippet of the event handler you want to test? I’m happy to help you write a test 👍 |
Sure; so from one handler I have this code: // media
Future<void> _onMediaAdded(MediaAdded event, Emitter<EditListingsState> emit) async {
// things here... nextState = state.copyWith ...;
emit(nextState);
await emit.forEach(
pipeline.process(event.media),
onData: (pe) {
add(ForwardedPipelineEvent(event: pe));
return state;
},
);
}
// then later
Future<void> _onForwardedPipelineEvent(ForwardedPipelineEvent forwarded, Emitter<EditListingsState> emit) async {
switch (forwarded.event) {
case final BranchSuccess bsuc:
// the pro tags should be displayed
if (bsuc.branchName != proTag) {
break;
}
// nextState = ...
break;
case final PipelineSuccess _:
// Mark the corresponding media as uploaded in the state
// nextState = ...
break;
default:
break;
}
emit(nextState);
// If the pipeline was successful, save the listing, as we probably have updated it with the new media
if (forwarded.event is PipelineSuccess) {
add(Save(listingId: lid, reason: 'onPipelineEvent(PipelineSuccess)'));
}
} |
Description
I'm trying to test how the state changes based on a stream that's listened to via
emit.ForEach
. However, the stream returned from theprocess
operation is never listened to in the test:Desired Solution
In a test situation, I'd expect
testBloc
to await all open emitters (the programmer should take care to ensure they close / complete).Alternatives Considered
I've tried to understand the mechanism by which this does not work; and it would seem that
bloc.close()
in the testing frameworks, cancels the broadcast stream before it's even consumed.The text was updated successfully, but these errors were encountered: