我有一个块来侦听身份验证事件的流(侦听 firebase 用户事件)。我的块是;
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();
}
}
提供用例是;
class CheckAuthenticationStatus
implements UseCaseListner<AuthenticationDetail> {
final AuthenticationRepository authenticationRepository;
CheckAuthenticationStatus({@required this.authenticationRepository});
@override
Stream<AuthenticationDetail> getStream() =>
authenticationRepository.getAuthDetailStream();
}
我正在尝试编写一个 bloc 测试,我可以在其中模拟用例并添加我自己的流,我可以将事件发送到如下;
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();
});
}
期望controller.add(authenticationDetail)
会产生这些事件,我希望去参加
mapEventToState
集团中的这些事件。然而,这并没有发生。
简而言之,我如何通过发送流事件而不是以编程方式使用 bloc.add() 事件来测试 bloc。