简体   繁体   English

Flutter Bloc 单元测试返回一个空数组

[英]Flutter Bloc Unit Test return an empty array

I'm trying to make a unit test using the bloc_test library.我正在尝试使用 bloc_test 库进行单元测试。

Here are my codes.这是我的代码。

Login Cubit登录肘

class LoginCubit extends Cubit<LoginState> with HydratedMixin {
  final UserRepository _userRepository;
  LoginCubit(this._userRepository) : super(LoginInitial());

  Future<void> login (String email, String password , bool remember) async {
    bool result = await _userRepository.isLoginCorrectWithEmailAndPassword(email, password);
    if (result){
      emit(LoggedIn(remember: remember, email: email));
    } else {
      emit(LoginError());
    }
  }
}

Login States登录状态

part of 'login_cubit.dart';

@immutable
abstract class LoginState extends Equatable {}

class LoginInitial extends LoginState {
  final bool remember;
  final String email;
  LoginInitial({this.remember = false, this.email = ''});

  @override
  List<Object?> get props => [remember, email];
}

class LoggedIn extends LoginState {
  final bool remember;
  final String email;
  LoggedIn({required this.remember, required this.email});

  @override
  List<Object?> get props => [remember, email];
}

class LoginError extends LoginState {
  LoginError();

  @override
  List<Object?> get props => [];
}

Unit Test单元测试

class MockUserRepository extends Mock implements UserRepository {
  @override
  Future<bool> isLoginCorrectWithEmailAndPassword(String email, String password) {
    return Future.value(true);
  }
}

void main() {
  group('LoginCubit', () {
    late LoginCubit loginCubit;

    setUp(() {
      loginCubit = LoginCubit(MockUserRepository());
    });

    tearDown(() {
      loginCubit.close();
    });

    test('the initial state value is LoginInitial', () {
      expect(loginCubit.state, LoginInitial());
    });

    blocTest<LoginCubit, LoginState>(
      'TODO: description',
      build: () => loginCubit,
      act: (cubit) => cubit.login("any email", "any password", true),
      expect: () => <LoginState>[
        LoggedIn(remember: true, email: "any email"),
      ],
    );
  });
}

My issue is that the second test return always an empty array.我的问题是第二个测试总是返回一个空数组。 With some prints, I'm sure that the code is emitting the LoggedIn states but the test actually don't recognize it.通过一些打印,我确信代码正在发出 LoggedIn 状态,但测试实际上无法识别它。

Where did I make a mistake?我在哪里做错了? :) :)

You forgot to add the stub for the repository function call.您忘记为存储库 function 调用添加存根。

  1. Create an Object from the Mock Class MockUserRepository repo = MockUserRepository();从 Mock Class MockUserRepository repo = MockUserRepository();

  2. Pass this object to the LoginCubit in the setUp function like this像这样将这个 object 传递给设置LoginCubit中的setUp

    setUp(() { loginCubit = LoginCubit(repo); });

  3. Add this line to the act function in the test将此行添加到测试中的act function

     when(repo.isLoginCorrectWithEmailAndPassword(any, any)).thenAnswer((_) async => true);

so the whole test should be like this所以整个测试应该是这样的

blocTest<LoginCubit, LoginState>(
  'TODO: description',
  build: () => loginCubit,
  act: (cubit) {
      when(repo.isLoginCorrectWithEmailAndPassword(any, any)).thenAnswer((_) async => true);
      cubit.login("any email", "any password", true);
  },
  expect: () => <LoginState>[
    LoggedIn(remember: true, email: "any email"),
  ],
);

The issue was that I was using an Hydrated Bloc without initializing it in the test.问题是我在测试中使用了 Hydrated Bloc 而没有对其进行初始化。 Here's the solution to it: https://github.com/felangel/bloc/issues/2022#issuecomment-747918417这是它的解决方案: https://github.com/felangel/bloc/issues/2022#issuecomment-747918417

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

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