ferryawijayanto
ferryawijayanto

Reputation: 649

RxSwift/RxTest how to test error response

i want to test error from api using RxTest but i don't know how to get the response value from the error.

func submitForm() {
        guard let denom = selectedDenom, let bal = accountBalance else { return }
        eventShowHideLoadingIndicator.onNext(true)
        manageCash.submitForm(cashType: cashType, denom: denom, accountBalance: bal)
            .subscribe(onFailure: { error in
                self.eventShowHideLoadingIndicator.onNext(false)
                guard let grpcError = error as? ErrorParser,
                      grpcError.type() == .NeedAuthorization,
                      !grpcError.authRequestId.isEmpty else {
                          self.errorHandler(error: error)
                          return }
                self.eventPinRequired.onNext(grpcError.authRequestId)
            }).disposed(by: disposeBag)
    }

This is how i came so far, the test failed because i can't get the value from error

describe("submitForm") {
                beforeEach {
                    mockManageCash.submitFormCashTypeDenomAccountBalanceReturnValue = Single.error(ErrorParser(grpcError: .NeedAuthorization))
                }
                context("when submit form return error") {
                    it("should return error NeedAuthorization") {
                        // Given
                        viewModel.selectedDenom = mockCashInquiryDenom
                        viewModel.accountBalance = mockAccountBalance
                        
                        let observer = scheduler.createObserver(String.self)
                        viewModel.rxEventPinRequired.subscribe(observer).disposed(by: disposedBag)

                        // When
                        viewModel.submitForm()

                        // Then
                        expect(observer.events.count).to(equal(1))
                    }
                }
            }

Upvotes: 1

Views: 208

Answers (1)

Daniel T.
Daniel T.

Reputation: 33967

You have a guard ahead of the eventPinRequired.onNext(_:) call. If your test is failing, it's because the guard is intercepting and returning.

Upvotes: 0

Related Questions