I am having issues with bloc_test, the expected does not match with the code.
**
Expected: [ AuthenticationState:AuthenticationState(RequestStatus.initial, RequestStatus.loading, RequestStatus.initial, null, null, null, null), AuthenticationState:AuthenticationState(RequestStatus.initial, RequestStatus.failed, RequestStatus.initial, null, An unknown error has occurred, null, null) ]
Actual: [ AuthenticationState:AuthenticationState(RequestStatus.loading, RequestStatus.initial, RequestStatus.initial, null, null, null, null), AuthenticationState:AuthenticationState(RequestStatus.failed, RequestStatus.initial, RequestStatus.initial, null, An unknown error has occurred, null, null) ]
Which: at location [0] is AuthenticationState:<AuthenticationState(RequestStatus.loading, RequestStatus.initial, RequestStatus.initial, null, null, null, null)> instead of AuthenticationState:<AuthenticationState(RequestStatus.initial, RequestStatus.loading, RequestStatus.initial, null, null, null, null)>
**
The above is the error bloc_test throws, even though from the code the expected is supposed to be the same as actual.
Below is the test
class MockAuthenticationRepository extends Mock
implements AuthenticationRepository {}
void main() {
late AuthenticationBloc authenticationBloc;
late MockAuthenticationRepository mockAuthenticationRepository;
setUp(() {
mockAuthenticationRepository = MockAuthenticationRepository();
authenticationBloc = AuthenticationBloc(
authenticationRepository: mockAuthenticationRepository);
});
tearDown(() => authenticationBloc.close());
group("login/signup with phone number", () {
blocTest("failed login/signup",
wait: const Duration(seconds: 1),
build: () => authenticationBloc,
act: (bloc) {
when(() => mockAuthenticationRepository
.signInWithPhoneNumber("123456789"))
.thenAnswer((invocation) async => const NetworkResponse(
status: RequestStatus.failed,
data: Constants.unknownErrorMessage));
bloc.add(const RequestOtp(phoneNumber: '123456789'));
},
expect: () => [
const AuthenticationState(
signInWithPhoneNumberStatus: RequestStatus.loading),
const AuthenticationState(
errorText: Constants.unknownErrorMessage,
signInWithPhoneNumberStatus: RequestStatus.failed)
]);
});
}
This is the state class
///The state class
class AuthenticationState extends Equatable {
final RequestStatus requestOtpStatus;
final RequestStatus signInWithPhoneNumberStatus;
final RequestStatus verifySignInWithPhoneNumberStatus;
final String? verificationId;
final String? errorText;
final int? resendToken;
final UserCredential? userCredential;
const AuthenticationState({
this.requestOtpStatus = RequestStatus.initial,
this.verifySignInWithPhoneNumberStatus = RequestStatus.initial,
this.signInWithPhoneNumberStatus = RequestStatus.initial,
this.verificationId,
this.errorText,
this.resendToken,
this.userCredential,
});
AuthenticationState copyWith({
RequestStatus? signInWithPhoneNumberStatus,
RequestStatus? verifySignInWithPhoneNumberStatus,
String? verificationId,
String? errorText,
int? resendToken,
UserCredential? userCredential,
}) {
return AuthenticationState(
requestOtpStatus:
signInWithPhoneNumberStatus ?? this.signInWithPhoneNumberStatus,
verifySignInWithPhoneNumberStatus: verifySignInWithPhoneNumberStatus ??
this.verifySignInWithPhoneNumberStatus,
verificationId: verificationId ?? this.verificationId,
errorText: errorText ?? this.errorText,
resendToken: resendToken ?? this.resendToken,
userCredential: userCredential ?? this.userCredential,
);
}
@override
List<Object?> get props => [
requestOtpStatus,
signInWithPhoneNumberStatus,
verifySignInWithPhoneNumberStatus,
verificationId,
errorText,
resendToken,
userCredential,
];
}
and the bloc
///The bloc
class AuthenticationBloc extends Bloc<OnboardingEvent, AuthenticationState> {
AuthenticationBloc({required this.authenticationRepository})
: super(const AuthenticationState()) {
on<RequestOtp>(_mapRequestOtpEventToState);
on<VerifyOtp>(_mapVerifyOtpEventToState);
on<ResendOtp>(_mapResendOtpEventToState);
}
final AuthenticationRepository authenticationRepository;
void _mapRequestOtpEventToState(
RequestOtp event, Emitter<AuthenticationState> emit) async {
emit(state.copyWith(signInWithPhoneNumberStatus: RequestStatus.loading));
final response =
await authenticationRepository.signInWithPhoneNumber(event.phoneNumber);
if (response.status != RequestStatus.success) {
final data = response.data;
emit(state.copyWith(
signInWithPhoneNumberStatus: response.status, errorText: data));
} else {
//success
final data = response.data as List<dynamic>;
emit(state.copyWith(
signInWithPhoneNumberStatus: response.status,
verificationId: data[0],
resendToken: data[1]));
}
}
}
I have asked chatgpt but it just keeps throwing my code right back to me, i don't understand why the state expected (from the log) does not match with my code but the actual is does.