I'm confused by the results of the test below
the first doesn't pass,
but somehow the second does not
what am I missing?
thank you
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 == '[email protected]') {
return const User(name: 'user', id: '12345');
}
}
}
logs
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.
CodePudding user response:
following 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()],
);