4

我正在使用 Freezed 在我的 Flutter 应用程序中生成密封数据类。我需要测试它,但我不知道该怎么做。任何想法?这是状态:

@freezed
abstract class LoginState with _$LoginState {
  const factory LoginState({
    @required Email email,
    @required Password password,
    @required bool isLoading,
    @required bool showErrors,
    @required Option<Either<AuthFailure, Unit>> failureOrSuccessOption,
  }) = _LoginState;

  factory LoginState.initial() => LoginState(
        failureOrSuccessOption: none(),
        isLoading: false,
        showErrors: false,
        email: Email(''),
        password: Password(''),
      );
}

这是我一直在尝试运行的测试:

import 'package:dartz/dartz.dart';
import 'package:exchangecontrol/auth/application/login_cubit/login_cubit.dart';
import 'package:exchangecontrol/auth/domain/auth_repository.dart';
import 'package:mockito/mockito.dart';
import 'package:flutter_test/flutter_test.dart';

class MockLoginWithEmailAndPassword extends Mock implements AuthRepository {}

void main() {
  LoginCubit loginCubit;
  MockLoginWithEmailAndPassword mockLoginWithEmailAndPassword;

  setUp(() {
    mockLoginWithEmailAndPassword = MockLoginWithEmailAndPassword();
    loginCubit = LoginCubit(mockLoginWithEmailAndPassword);
  });
  test('Should be able to test login', () async {
    when(mockLoginWithEmailAndPassword.loginWithEmailAndPassword(any, any))
        .thenAnswer((_) async => right(unit));
    loginCubit.loginButtonPressed();
    expectLater(
      loginCubit,
      emitsInOrder(
        [
          loginCubit.state.copyWith(isLoading:true),
          loginCubit.state.copyWith(isLoading:false),
        ],
      ),
    );
  });
}

我想检查,在执行 cubit loginMethod 时,它是否正确地将加载状态更改为 true(同时获得结果)和 false(获得结果之后),但我收到以下错误消息:

Expected: should do the following in order:
          * emit an event that _$_LoginState:<LoginState(email: Value(Left(EmailFailure.empty())), password: Value(Left(PasswordFailure.empty())), isLoading: true, showErrors: false, failureOrSuccessOption: None)>
          * emit an event that _$_LoginState:<LoginState(email: Value(Left(EmailFailure.empty())), password: Value(Left(PasswordFailure.empty())), isLoading: false, showErrors: false, failureOrSuccessOption: None)>
  Actual: _$_LoginState:<LoginState(email: Value(Left(EmailFailure.empty())), password: Value(Left(PasswordFailure.empty())), isLoading: false, showErrors: false, failureOrSuccessOption: None)>
   Which: was not a Stream or a StreamQueue
4

0 回答 0