在 Flutter Mockito BlocTest 中找不到初始状态
我正在尝试做一个非常简单的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是
bloc_test
包中的expect
属性文档:这意味着,在
expect
回调中,您应该只放置发出的 状态。初始状态是初始状态,在将事件添加到 BLoC 后不会发出它。如果您想验证 BLoC 的初始状态,您可以为其编写一个单独的测试:
This is the
expect
property documentation inbloc_test
package: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: