How can I mock the function [Flutter Mockito]

68 Views Asked by At

I have method in api

  Future<Either<ApiErrorDto, AuthSignInDto>> signIn({
     required final String login,
     required final String password,
 }) async {
   final data = await _apiDataProvider.signIn(login: login, password: password);
   if (data.isLeft) {
     return Left(ApiErrorDto.fromApi(apiError: data.left));
   }
   return Right(AuthSignInDto.fromApi(response: data.right));
}

I use it in AuthBloc

 FutureOr<void> _onAuthPageSignIn(
     final AuthPageSignIn event, final Emitter<AuthState> emit) async {
   final data = await api.signIn(
      login: event.login,
      password: event.password);
   if (data.isRight) {
      emit(AuthDoneState());
   } else {
      emit(AuthErrorState(apiError: data.left));
   }
}

I try to write the unit test on this

blocTest<AuthBloc, AuthState>(
  "Auth BLoC's emit AuthDoneState() when api request happens successfully ",
  build: () {
    when(api.signIn(login: '', password: '')).thenAnswer((_) =>
        Future<Either<ApiErrorDto, AuthSignInDto>>.value(const Right(
            AuthSignInDto(token: 'token', username: 'username'))));
    return authBloc;
  },
  act: (bloc) {
    bloc.add(const AuthPageSignIn(login: 'admin', password: 'admin'));
  },
  expect: () => <AuthState>[
    AuthDoneState(),
  ],
);

I get the error

MissingDummyValueError: Either<ApiErrorDto, AuthSignInDto>

This means Mockito was not smart enough to generate a dummy value of type 'Either<ApiErrorDto, AuthSignInDto>'. Please consider using either 'provideDummy' or 'provideDummyBuilder' functions to give Mockito a proper dummy value.

Please note that due to implementation details Mockito sometimes needs users to provide dummy values for some types, even if they plan to explicitly stub all the called methods.

How can I provide dummyvalue for Either<ApiErrorDto, AuthSignInDto> ?

0

There are 0 best solutions below