Giuseppe Pennisi
Giuseppe Pennisi

Reputation: 428

How to test a WKWebView using RxWebKit?

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?

Upvotes: -1

Views: 368

Answers (1)

Adis
Adis

Reputation: 4552

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:

  1. RxTest: https://github.com/ReactiveX/RxSwift/blob/master/Documentation/UnitTests.md

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"])
}
  1. or you can use RxBlocking: https://github.com/ReactiveX/RxSwift/tree/master/RxBlocking

For example, here's a good point to start:

http://rx-marin.com/post/rxblocking-part1/

Upvotes: 0

Related Questions