Here's the implicated methods of the cubit I'm testing:
void authCheck() {
if (_authUser != null) {
emit(AuthState.authenticated(_authUser!));
if (kDebugMode) {
// ignore: avoid_print
print(_authUser!);
}
} else {
emit(const AuthState.unauthenticated());
signInAnonymously();
}
}
Future<void> signInAnonymously() async {
final either = await _authRepository.signInAnonymously();
either.fold(
(failure) => _showFailureToast(failure),
(auth) => emit(AuthState.authenticated(_authUser!)),
);
}
I'm testing the unauthenticated part of authCheck
, here's the test:
blocTest('should emit [Unauthenticated] when user is not authenticated',
build: () {
when(authCubit.signInAnonymously())
.thenAnswer((_) => Future.value(null));
when(mockIAuthRepository.getSignedInUser()).thenReturn(null);
return authCubit;
},
act: (AuthCubit authcubit) {
authcubit.authCheck();
},
expect: () => [const AuthState.unauthenticated()]);
But when stubbing authCubit.signInAnonymously
to return a Future<void>
I get this error:
UnimplementedError: fold
package:test_api Fake.noSuchMethod
package:dartz/src/either.dart 8:5 _FakeEither_0.fold
package:mage_duel/application/auth/auth_cubit.dart 38:12 AuthCubit.signInAnonymously
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
test/application/auth/auth_cubit_test.dart 51:26 main.<fn>.<fn>
package:bloc_test/src/bloc_test.dart 196:29 testBloc.<fn>.<fn>
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:bloc_test/src/bloc_test.dart testBloc.<fn>.<fn>
dart:async runZonedGuarded
package:bloc_test/src/bloc_test.dart 192:13 testBloc.<fn>
package:bloc_test/src/bloc_test.dart 191:5 testBloc.<fn>
dart:async runZoned
package:bloc/src/bloc_overrides.dart 46:26 BlocOverrides.runZoned
package:bloc_test/src/bloc_test.dart 190:23 testBloc
package:bloc_test/src/bloc_test.dart 156:13 blocTest.<fn>
package:bloc_test/src/bloc_test.dart 155:5 blocTest.<fn>
type 'Future<void>' is not a subtype of type 'Future<Either<AuthFailure, Unit>>' in type cast
test/application/auth/auth_cubit_test.mocks.dart 50:11 MockIAuthRepository.signInAnonymously
package:mage_duel/application/auth/auth_cubit.dart 37:42 AuthCubit.signInAnonymously
package:mage_duel/application/auth/auth_cubit.dart 32:7 AuthCubit.authCheck
test/application/auth/auth_cubit_test.dart 57:21 main.<fn>.<fn>
package:bloc_test/src/bloc_test.dart 201:24 testBloc.<fn>.<fn>
===== asynchronous gap ===========================
dart:async _completeOnAsyncError
package:mage_duel/application/auth/auth_cubit.dart AuthCubit.signInAnonymously
package:mage_duel/application/auth/auth_cubit.dart 32:7 AuthCubit.authCheck
test/application/auth/auth_cubit_test.dart 57:21 main.<fn>.<fn>
package:bloc_test/src/bloc_test.dart 201:24 testBloc.<fn>.<fn>
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:bloc_test/src/bloc_test.dart testBloc.<fn>.<fn>
dart:async runZonedGuarded
package:bloc_test/src/bloc_test.dart 192:13 testBloc.<fn>
package:bloc_test/src/bloc_test.dart 191:5 testBloc.<fn>
dart:async runZoned
package:bloc/src/bloc_overrides.dart 46:26 BlocOverrides.runZoned
package:bloc_test/src/bloc_test.dart 190:23 testBloc
package:bloc_test/src/bloc_test.dart 156:13 blocTest.<fn>
package:bloc_test/src/bloc_test.dart 155:5 blocTest.<fn>