-
-
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
blocTest does not catch all emitted states #724
Comments
Hi @MohsinN 👋 I'll take a look shortly 👍 |
Hey sorry for the delayed response (just got back from vacation). I took a closer look and this is actually working as expected. You can instead refactor the code like: import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:flutter_login/authentication/authentication.dart';
import 'package:meta/meta.dart';
import 'package:user_repository/user_repository.dart';
class AuthenticationBloc
extends Bloc<AuthenticationEvent, AuthenticationState> {
final UserRepository userRepository;
AuthenticationBloc({@required this.userRepository})
: assert(userRepository != null);
@override
AuthenticationState get initialState => AuthenticationUninitialized();
@override
Stream<AuthenticationState> mapEventToState(
AuthenticationEvent event,
) async* {
if (event is AppStarted) {
final bool hasToken = await userRepository.hasToken();
if (hasToken) {
yield AuthenticationAuthenticated();
} else {
yield AuthenticationUnauthenticated();
}
}
if (event is LoggedIn) {
yield AuthenticationLoading();
await userRepository.persistToken(event.token);
yield AuthenticationAuthenticated();
// do logout logic directly or extract into a private helper function
await userRepository.deleteToken();
yield AuthenticationUnauthenticated();
}
if (event is LoggedOut) {
yield AuthenticationLoading();
await userRepository.deleteToken();
yield AuthenticationUnauthenticated();
}
}
} Hope that helps 👍 |
Hi, Thank you for the detailed response. Great work with library. Also, please let me know if I can contribute in any way. I can help out in writing tests if you like. Open to other suggestions as well. Do let me know what you think. |
No problem! If you find anything you feel is lacking please don't hesitate to open an issue and create a pull request 👍 Thanks so much! 🙏 |
Awesome thanks! 😄 |
The issue was that emitsExactly was listening to events too late. Since Bloc uses a BehaviorSubject, if there is a delay between events the listener would miss states emitted before the listener attaches. The listener should start listening before any events are added so that all states get recorded. I suspect this was the underlying problem with [felangel#724].
@felangel From your comment above:
Is it a general rule? In other words, is it an anti-pattern to invoke Thanks in advance for your advice! |
bloc_test
does not catch states emitted by consecutive events added inside bloc. Same scenario passes in plain test but fails withblocTest
.I updated your
flutter_login
sample to replicate the issue.Updated
AuthenticationBloc
to addLoggedOut
event inside case ofLoggedIn
In the first case, only first three states are caught by
blocTest
Updated tests:
The text was updated successfully, but these errors were encountered: