Bloc test issue in flutter

23 Views Asked by At

Want to test this bloc method, Using freezed bloc architecture.

FutureOr<void> _debitAmount(
    _DebitAmount event,
    Emitter<FeePaymentState> emit,
  ) async {
    try {
      emit(state.copyWith(isBusy: true));

      final debitAmountResponseModel =
          await _repository.debitAmount(event.body);

      VerifyServiceFeeRequestModel verifyServiceFeeRequestModel =
          VerifyServiceFeeRequestModel(
        transactionId: num.parse(event.body.moduleTransactionId!),
        amount: event.body.amount!,
        walletTxnId:
            num.parse(debitAmountResponseModel.data!.walletTransactionId!),
        moduleId: 12,
        recordType: event.body.recordType,
        paymentReceiptNo: event.body.receiptNo,
      );

      final verifyService =
          await _repository.verifyService(verifyServiceFeeRequestModel);

      emit(
        state.copyWith(
            isBusy: false,
            debitAmountResponseModel: debitAmountResponseModel,
            verifyServiceFeeResponseModel: verifyService,
            notification: _NotifySuccess(message: 'debitAmountResponseModel'),
            status: const UILoadSuccess()),
      );
    } catch (e, s) {
      _log.e('Load failed', e, s);
      emit(
        state.copyWith(
          isBusy: false,
          notification: _NotifyFailed(
            message:
                e is DioException ? e.response!.data['message'] : e.toString(),
          ),
        ),
      );
    }
  }

Unit test case

 group('_DebitAmount', () {
    const response = DebitAmountResponseModel();
    const verifyResponse = VerifyServiceFeeResponseModel();
    const body = DebitAmountRequestModel();
    VerifyServiceFeeRequestModel verifyServiceFeeRequestModel =
        const VerifyServiceFeeRequestModel();

    blocTest<FeePaymentBloc, FeePaymentState>(
      '_DebitAmount successful',
      build: () => feePaymentBloc,
      setUp: () {
        when(mockFeePaymentRepository.debitAmount(body)).thenAnswer(
          (_) => Future<DebitAmountResponseModel>.value(response),
        );
        when(mockFeePaymentRepository
                .verifyService(verifyServiceFeeRequestModel))
            .thenAnswer(
          (_) => Future<VerifyServiceFeeResponseModel>.value(verifyResponse),
        );
      },
      act: (bloc) async {
        bloc.add(const FeePaymentEvent.debitAmount(body));
      },
      expect: () => [
        isA<FeePaymentState>().having(
          (state) => state.isBusy,
          'isBusy',
          true,
        ),
        isA<FeePaymentState>()
            .having(
              (state) => state.isBusy,
              'isBusy',
              false,
            )
            .having(
              (state) => state.debitAmountResponseModel,
              'debitAmountResponseModel',
              response,
            )
            .having(
              (state) => state.verifyServiceFeeResponseModel,
              'verifyServiceFeeResponseModel',
              verifyResponse,
            )
            .having(
              (state) => state.status,
              'status',
              const UILoadSuccess(),
            ),
      ],
    );

}

Don't understand how to read this error

Expected: [
            <<Instance of 'FeePaymentState'> with `isBusy`: <true>>,
            <<Instance of 'FeePaymentState'> with `isBusy`: <false> and `debitAmountResponseModel`: _$DebitAmountResponseModelImpl:<DebitAmountResponseModel(status: null, success: null, count: null, data: null, type: null)> and `verifyServiceFeeResponseModel`: _$VerifyServiceFeeResponseModelImpl:<VerifyServiceFeeResponseModel(status: null, success: null, count: null, data: null, type: null)> and `status`: _$UILoadSuccessImpl:<UIStatus.loadSuccess(message: null)>>
          ]
  Actual: [
            _$FeePaymentStateImpl:FeePaymentState(status: UIStatus.initial(), notification: null, isBusy: true, isNavigated: false, walletInfoResponseModel: null, walletBalanceResponseModel: null, debitAmountResponseModel: null, verifyServiceFeeResponseModel: null),
            _$FeePaymentStateImpl:FeePaymentState(status: UIStatus.initial(), notification: FeePaymentNotification.notifyFailed(message: Null check operator used on a null value), isBusy: false, isNavigated: false, walletInfoResponseModel: null, walletBalanceResponseModel: null, debitAmountResponseModel: null, verifyServiceFeeResponseModel: null)
          ]
   Which: at location [1] is _$FeePaymentStateImpl:<FeePaymentState(status: UIStatus.initial(), notification: FeePaymentNotification.notifyFailed(message: Null check operator used on a null value), isBusy: false, isNavigated: false, walletInfoResponseModel: null, walletBalanceResponseModel: null, debitAmountResponseModel: null, verifyServiceFeeResponseModel: null)> which has `debitAmountResponseModel` with value <null>
0

There are 0 best solutions below