块测试输出意外结果



我被下面的测试结果弄糊涂了

第一个不通过,

但是第二个却没有

我错过了什么?

谢谢


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()],
);

相关内容

  • 没有找到相关文章

最新更新