-1

これは私のCubitクラスをテストするための最初の試みです。何時間も試した後、完全にアイデアがなくなりましたので、ご容赦ください。

次のような単純なキュビットをテストしようとしています。

@injectable
class ResetPasswordCubit extends Cubit<ResetPasswordState> {

  ResetPasswordCubit() : super(ResetPasswordInitial());

  void attemptPasswordReset({required ResetPasswordParams params}) async {
    emit(ResetPasswordLoading());
    emit(ResetPasswordLoaded());
  }
}

私がやりたいのは、これらの状態が両方とも順番に出力されたことを確認することだけです。docsによると、これを行うにはいくつかの方法がありますが、どれを使用すればよいかさえわかりません。両方を試しましたが、単体テストを使用したいと思います。ここに私が持っているものがあります:

class MockResetPasswordCubit extends MockCubit<ResetPasswordState>
    implements ResetPasswordCubit {}

@GenerateMocks([ResetPassword])
void main() {
  late MockResetPassword mockResetPassword;
  late MockResetPasswordCubit cubit;
  late ResetPasswordParams params;

  setUp(() {
    mockResetPassword = MockResetPassword();
    cubit = MockResetPasswordCubit();
    params = const ResetPasswordParams(
        pin: "1234", password: "hello", confirmPassword: "hello");
    when(mockResetPassword.call(params))
        .thenAnswer((_) async => const Right(ResetPasswordResult.validated));
  });

  blocTest<ResetPasswordCubit, ResetPasswordState>(
      'when attempt to validate password is made then loading state is emitted',
      build: () => cubit,
      act: (cubit) => cubit.attemptPasswordReset(params: params),
      expect: () => [ResetPasswordLoading(), ResetPasswordLoaded()]);
}

そして、これは表示されるエラーです:

予想: [' ResetPasswordLoading ' のインスタンス、'ResetPasswordLoaded' のインスタンス
]

私は本当にアイデアが足りないので、誰かが私を正してくれることを願っています。ありがとう。

4

1 に答える 1