I'm trying to write some tests for my application, that uses rxSwift. In particular, I'd like to test, writing unit tests, a webview. I'm using RxWebKit to get observables over some properties like navigationCompleted or NavigationFailed and so on.
For example: webView.rx.didFailNavigation.asDriver()
. These observables are given as input to my viewModel.
But i'm not sure how to write these tests to simulate, for example, a failed navigation and so an emission of this kind of observable.
In the specific case, i want to simulate a Driver<(webView: WKWebView, navigation: WKNavigation, error: Error)>
(that is the same type of the one associated to webView.rx.didFailNavigation.asDriver()
).
I understood that to simulate the emission i need to create a scheduler and call the createHotObservable
method, but what have I to pass to Recorderd.next(150, element)
as element in the specific case to simulate the fail of the webview?
can someone give me a simple example?
Asked
Active
Viewed 297 times
-1

Giuseppe Pennisi
- 396
- 3
- 22
1 Answers
0
This is a bit too general question so I'll give a more general answer:
You need to look into testing with RxSwift, either using:
for example:
func testElementsEmitted() {
let scheduler = TestScheduler(initialClock: 0)
let xs = scheduler.createHotObservable([
.next(210, "RxSwift"),
.next(220, "is"),
.next(230, "pretty"),
.next(240, "awesome")
])
let res = scheduler.start { xs.asObservable() }
XCTAssertRecordedElements(res.events, ["RxSwift", "is", "pretty", "awesome"])
}
- or you can use RxBlocking: https://github.com/ReactiveX/RxSwift/tree/master/RxBlocking
For example, here's a good point to start:

Adis
- 4,512
- 2
- 33
- 40
-
thanks for the answer, I added some details to address better my problem. – Giuseppe Pennisi Jul 21 '20 at 15:49
-
If you want to pass an error, then do it as .error() the same way you would emit a next element in sequence. – Adis Jul 21 '20 at 15:53