在 Flutter Mockito BlocTest 中找不到初始状态

发布于 2025-01-12 15:03:24 字数 1538 浏览 2 评论 0原文

我正在尝试做一个非常简单的 blocTest 来测试初始状态。但低于错误。有趣的是,我确实有另一个工作组并在同一个项目中进行测试。我逐行检查以查看是否有任何不匹配,但一切看起来都很完美,除了“行为”之外,我正在其他地方而不是这里做,假设与此初始状态测试无关。知道为什么这个集团会发生这种情况吗?

Expected: [ReadyToAuthenticateState:ReadyToAuthenticateState()]
  Actual: []
   Which: at location [0] is [] which shorter than expected

我的块测试

  late MockUserAuthenticationUseCase mockUsecase;
  late UserAuthenticationBloc authBloc;
  setUp(() {
    mockUsecase = MockUserAuthenticationUseCase();
    authBloc = UserAuthenticationBloc(usecase: mockUsecase);
  });

blocTest<UserAuthenticationBloc, UserAuthenticationState>(
    'emits [MyState] when MyEvent is added.',
    build: () => authBloc,
    expect: () => <UserAuthenticationState>[ReadyToAuthenticateState()],
  );

我的块

class UserAuthenticationBloc
    extends Bloc<UserAuthenticationEvent, UserAuthenticationState> {
  final UserAuthenticationUseCase usecase;

  UserAuthenticationBloc({required this.usecase})
      : super(ReadyToAuthenticateState()) {
    on<UserAuthenticationEvent>((event, emit) {
      if (event is AuthenticateUserWithCredentialsEvent) {
        _processReadyToAuthenticateEvent(event);
      }
    });
  }

  void _processReadyToAuthenticateEvent(
      AuthenticateUserWithCredentialsEvent event) async {
    await usecase(
        UserAuthenticationUseCaseParams(event.username, event.password));
  }
}

更新#1:我也将初始状态期望插入到其他工作块测试中,并得到了相同的错误。看来我们不应该测试初始状态。

I am trying to do a very simple blocTest to test initial state. But getting below error. Interestingly I do have another working bloc and test in the same project. I checked line by line to see any mismatch, but everything looks perfect, except the 'act', which I am doing in the other, not here, assuming not relevant for this initial state test. Any idea, why this is happening to this bloc?

Expected: [ReadyToAuthenticateState:ReadyToAuthenticateState()]
  Actual: []
   Which: at location [0] is [] which shorter than expected

My bloc test

  late MockUserAuthenticationUseCase mockUsecase;
  late UserAuthenticationBloc authBloc;
  setUp(() {
    mockUsecase = MockUserAuthenticationUseCase();
    authBloc = UserAuthenticationBloc(usecase: mockUsecase);
  });

blocTest<UserAuthenticationBloc, UserAuthenticationState>(
    'emits [MyState] when MyEvent is added.',
    build: () => authBloc,
    expect: () => <UserAuthenticationState>[ReadyToAuthenticateState()],
  );

My bloc

class UserAuthenticationBloc
    extends Bloc<UserAuthenticationEvent, UserAuthenticationState> {
  final UserAuthenticationUseCase usecase;

  UserAuthenticationBloc({required this.usecase})
      : super(ReadyToAuthenticateState()) {
    on<UserAuthenticationEvent>((event, emit) {
      if (event is AuthenticateUserWithCredentialsEvent) {
        _processReadyToAuthenticateEvent(event);
      }
    });
  }

  void _processReadyToAuthenticateEvent(
      AuthenticateUserWithCredentialsEvent event) async {
    await usecase(
        UserAuthenticationUseCaseParams(event.username, event.password));
  }
}

Update #1: I inserted initial state expectation also to the other working blocTest and got the same error. Seems we are not expected to test initial state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘香 2025-01-19 15:03:24

这是 bloc_test 包中的 expect 属性文档:

/// [expect] is an optional `Function` that returns a `Matcher` which the `bloc`
/// under test is expected to emit after [act] is executed.

这意味着,在 expect 回调中,您应该只放置发出的 状态。初始状态是初始状态,在将事件添加到 BLoC 后不会发出它。

如果您想验证 BLoC 的初始状态,您可以为其编写一个单独的测试:

test('should set initial state', () {
  expect(authBloc.state, ReadyToAuthenticateState());
});

This is the expect property documentation in bloc_test package:

/// [expect] is an optional `Function` that returns a `Matcher` which the `bloc`
/// under test is expected to emit after [act] is executed.

Meaning, inside the expect callback, you should put only the emitted states. Initial state is, well, the initial one, it is not emitted after you add an event to the BLoC.

If you want to verify an initial state of the BLoC, you can write a separate test for it:

test('should set initial state', () {
  expect(authBloc.state, ReadyToAuthenticateState());
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文