i have a bloc that listens to a stream for authentication events (listening for firebase user events). my block is;
class AuthenticationBloc
extends Bloc<AuthenticationEvent, AuthenticationLoadingState> {
StreamSubscription<AuthenticationDetail> streamSubscription;
CheckAuthenticationStatus authenticationStatus;
AuthenticationBloc({@required CheckAuthenticationStatus authenticationStatus})
: assert(authenticationStatus != null),
assert(authenticationStatus.getStream() != null),
this.authenticationStatus = authenticationStatus,
super(AuthenticationLoadingState().init()) {
this.streamSubscription = this
.authenticationStatus
.getStream()
.listen((AuthenticationDetail detail) async* {
print(detail.toString());
add(StatusChanged(detail));
});
}
@override
Stream<AuthenticationLoadingState> mapEventToState(
AuthenticationEvent event) async* {
if (event is ListenToAuthenticationEvents) {
print('well well well');
} else if (event is StatusChanged) {
print('yeeee');
}
}
@override
Future<void> close() {
this.streamSubscription?.cancel();
return super.close();
}
Future<AuthenticationLoadingState> init() async {
return state.clone();
}
}
And the providing use case is;
class CheckAuthenticationStatus
implements UseCaseListner<AuthenticationDetail> {
final AuthenticationRepository authenticationRepository;
CheckAuthenticationStatus({@required this.authenticationRepository});
@override
Stream<AuthenticationDetail> getStream() =>
authenticationRepository.getAuthDetailStream();
}
Im trying to write a bloc test where i can mock the usecase and add my own stream which i can send events to as follows;
class MockCheckAuthenticationStatus extends Mock
implements CheckAuthenticationStatus {}
void main() {
MockCheckAuthenticationStatus authenticationStatus;
AuthenticationBloc authenticationBloc;
StreamController controller;
Stream<AuthenticationDetail> stream;
setUp(() {
controller = StreamController<AuthenticationDetail>();
stream = controller.stream;
authenticationStatus = MockCheckAuthenticationStatus();
});
test('initial state is correct', () async {
var authenticationDetail = AuthenticationDetail(isValid: true);
when(authenticationStatus.getStream()).thenAnswer((_) => stream);
authenticationBloc =
AuthenticationBloc(authenticationStatus: authenticationStatus);
//this should action, but doesnt, why?
controller.add(authenticationDetail);
await untilCalled(authenticationStatus.getStream());
verify(authenticationStatus.getStream());
});
tearDown(() {
authenticationBloc?.close();
controller?.close();
});
}
the expectation is that controller.add(authenticationDetail)
will generate the events and i expect to go to the
mapEventToState
on those events in the bloc. this is however not happening.
In short, how can i test the bloc by sending it stream events rather than programatically using bloc.add() events.