flutter - bloc 测试输出意外结果
问题描述
我对下面的测试结果感到困惑
第一个没有通过,
但不知何故,第二个没有
我错过了什么?
谢谢你
void main() {
group('UserAuthenticationCubit', () {
const user = User(name: 'name', id: 'id');
final repo = UserAuthenticationRepoMock();
final cubit = UserAuthenticationCubit(repo);
blocTest<UserAuthenticationCubit, UserBase>(
'emits [] when nothing is called',
build: () => UserAuthenticationCubit(repo),
expect: () => const <UserBase>[],
);
blocTest<UserAuthenticationCubit, UserBase>(
'emits NoUser when repo returns null',
setUp: () {
when(() => repo(kFakeUserCredentials)).thenAnswer(
(_) => Future.sync(() => null),
);
},
build: () => cubit,
act: (cubit) => cubit.authenticate(kFakeUserCredentials),
expect: () => const [MaybeUser(), NoUser()],
);
blocTest<UserAuthenticationCubit, UserBase>(
'emits User when repo returns User',
setUp: () {
when(() => repo(kFakeUserCredentials)).thenAnswer(
(_) => Future.sync(() => user),
);
},
build: () => UserAuthenticationCubit(repo),
act: (cubit) => cubit.authenticate(kFakeUserCredentials),
expect: () => const [MaybeUser(), user],
);
tearDown(() => cubit.close());
});
}
@immutable
class User implements UserBase {
final String name, id;
const User({
required this.name,
required this.id,
});
}
abstract class UserBase {}
class MaybeUser implements UserBase {
const MaybeUser();
}
class NoUser implements UserBase {
const NoUser();
}
class UserAuthenticationCubit extends Cubit<UserBase> {
final UserAuthenticationRepo userAuthenticationRepo;
UserAuthenticationCubit(this.userAuthenticationRepo) : super(const NoUser());
void authenticate(Credentials credentials) async {
emit(const MaybeUser());
final user = await userAuthenticationRepo(credentials);
user == null ? emit(const NoUser()) : emit(user);
}
}
class UserAuthenticationRepo {
Future<User?> call(Credentials creadentials) async {
if (creadentials.email == 'test@test.com') {
return const User(name: 'user', id: '12345');
}
}
}
日志
00:02 +7 -1: /Users/francesco/development/flutter-tools/template/bloc_template/test/logic/cubit/authentication_test.dart: UserAuthenticationCubit emits NoUser when repo returns null [E]
Expected: [Instance of 'MaybeUser', Instance of 'NoUser']
Actual: []
Which: at location [0] is [] which shorter than expected
package:test_api expect
package:bloc_test/src/bloc_test.dart 193:9 testBloc.<fn>
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:bloc_test/src/bloc_test.dart testBloc.<fn>
dart:async runZonedGuarded
package:bloc_test/src/bloc_test.dart 172:9 testBloc
package:bloc_test/src/bloc_test.dart 140:11 blocTest.<fn>
package:bloc_test/src/bloc_test.dart 139:26 blocTest.<fn>
00:02 +9 -1: Some tests failed.
解决方案
关注https://github.com/felangel/bloc/issues/2814
blocTest<UserAuthenticationCubit, UserBase>(
'emits NoUser when repo returns null',
setUp: () {
when(() => repo(kFakeUserCredentials)).thenAnswer(
(_) => Future.sync(() => null),
);
},
# build: () => cubit,
build: () => UserAuthenticationCubit(repo),
act: (cubit) => cubit.authenticate(kFakeUserCredentials),
expect: () => const [MaybeUser(), NoUser()],
);
推荐阅读
- alexa - Alexa DateSlot 摩根 vs. 摩根
- excel - 根据单元格值更改形状数量
- apache-kafka - Kafka Streams:将一个主题传递给另一个主题
- java - 使用打开的 csv 时从 csv 中删除双引号
- c# - MVC“模型”引发了“System.NullReferenceException”类型的异常
- sql - 多列但只有一个连接
- android - Android Studio 3.3.1、Anko 支持 0.10.7:出现灰屏且无预览
- pandoc - 引文风格语言 - 扩展附加字段
- python - 如何用url替换输入
- ansible - 最佳实践:如果 Ansible 剧本中有弃用警告,如何使 CI 管道失败?