4

Flutter アプリで封印されたデータ クラスを生成するために Freezed を使用しています。テストする必要がありますが、その方法がわかりません。何か案が?これは状態です:

@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