Reputation: 3
I'm writing unit test for my bloc event's. And some event's triggered other event's inside itself. I used expect to test state changes. But when nested event't triggered expect expects state change's inside nested event.
How to test them ?
First event with nested one :
if (result.right.success) {
emit(
state.copyWith(
phone: event.phoneNumber,
isNewUser: false,
loginStatus: FormzSubmissionStatus.success,
email: result.right.email,
loginResponseEntity: result.right,
isInternationalPhone: state.selectedCountry.countryCode.toLowerCase() != "uz"),
);
**add(GetVerificationCode());**
;
blocTest<AuthBloc, AuthState>(
"should check phone number and continue as a registered user with local phone number",
build: () => authBloc,
setUp: () {
when(() => checkPhoneUseCaseMock.call(tPhoneNumber)).thenAnswer((_) async => Right(tCheckResponseEntity));
},
act: (bloc) {
bloc.add(CheckPhone(phoneNumber: tPhoneNumber));
},
expect: () => [
isA<AuthState>().having((state) => state.loginStatus, "progress state", FormzSubmissionStatus.inProgress),
isA<AuthState>()
.having((state) => state.loginStatus, "loginStatus", FormzSubmissionStatus.success)
.having((state) => state.isNewUser, "isNewUser", false)
.having((state) => state.phone, "phone", tPhoneNumber)
.having((state) => state.loginResponseEntity, "loginResponseEntity", tCheckResponseEntity)
.having((state) => state.isInternationalPhone, "isInternationalPhone", false)
.having((state) => state.email, "email", ""),
isA<AuthState>()
.having((state) => state.loginStatus, "loginStatus", FormzSubmissionStatus.initial)
.having((state) => state.message, "error message", ""),
],
verify: (bloc) {
// check phone number is called once
verify(() => checkPhoneUseCaseMock.call(tPhoneNumber)).called(1);
final mockBloc = bloc as AuthBlocMock;
expect(mockBloc.capturedEvents, contains(isA<GetVerificationCode>()));
},
);
Upvotes: 0
Views: 17