简体   繁体   English

集团测试 output 意外结果

[英]bloc test output unexpected result

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 == 'test@test.com') {
      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.

following https://github.com/felangel/bloc/issues/2814以下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()],
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM