0

我有一个块来侦听身份验证事件的流(侦听 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。

4

1 回答 1

0

问题的前提是基于官方 Firebase 身份验证文档,其中指出

Events are fired when the following occurs:

 - Right after the listener has been registered. 
 - When a user is signed in. 
 - When the current user is signed out.

所以不是注册一个 listener ,而是通过一个事件调用 getUser 。只需注册侦听器也应该触发第一个事件,然后我们可以使用该事件进行相应的导航。我测试的目的是模拟第一个事件。

代码中的错误是在 AuthenticationBloc 构造函数中async*的行.listen((AuthenticationDetail detail) async* {

于 2021-07-13T13:55:03.453 回答