We are currently implementing unit tests for our ViewModel.
When a requestInAppPayment Input request comes in from the view model, subscription is implemented only when the instance property isPurchasing filter is false (to prevent multiple touches on the purchase button).
I want to write a test case for the filter when the purchase button is pressed while the purchase is in progress, but I don't know how to do it.
I want to write a test case that filters when the purchase button is clicked while the purchase is in progress.
in ViewMode
requestInAppPayment
.filter { [weak self] _ in
self?.isPurchasing == false
}
.subscribe(with: self, onNext: { owner, inAppPayment in
owner.isPurchasing = true
owner.showIndicator.onNext(())
owner.requestInAppPurchaseUseCase.execute(productID: inAppPayment.productID)
})
.disposed(by: disposeBag)
requestInAppPurchaseUseCase.purchaseSuccess
.do(onNext: { [weak self] _ in
self?.isPurchasing = false
self?.hideIndicator.onNext(())
})
in Unit Test
let observer = scheduler.createObserver(String.self)
let stubPaymentItem = InAppPayment()
scheduler.createHotObservable([
.next(10, stubPaymentItem),
.next(20, stubPaymentItem)
])
.bind(to: viewModel.input.requestInAppPurchase)
.disposed(by: disposeBag)
viewModel.output.showPaymentResultPage
.map { "success" }
.drive(observer)
.disposed(by: disposeBag)
scheduler.start()
// result = [.next(10, "success"), .next(20, "success")],
// The logic I think is [.next(10, "success")]
XCTAssertEqual(observer.events, [
.next(10, "success")
])
Here is a fully working example based on what little you provided of the unit test alone. I did not use your view model code at all, I just wrote a view model that passes the test.
I used the test harness I have available here: https://gist.github.com/danielt1263/bd449100764e3166644f7a38bca86c96
Here is the production code to make the above work: