Reputation: 404
I want to write unit test cases (XCTest) in UIWebView/ Webkit- Swift Please post any helpful link, example, or tutorial.
Thank you. Shriram
Upvotes: 4
Views: 2805
Reputation: 4107
A good way is to create fake navigation actions to call manually the delegate.
In this question you have a good example to write test cases of this way. unit-testing-wknavigationdelegate-functions
Example to test loading in navigation:
// setup
let fakeNavigation = WKNavigation()
delegateObject.refresh() // Set loading to true and init the web view
XCTAssertTrue(delegateObject.loading)
delegateObject.webView(webView, didFinish: fakeNavigation)
XCTAssertFalse(delegateObject.loading)
Example to test the policy:
class FakeNavigationAction: WKNavigationAction {
let testRequest: URLRequest
override var request: URLRequest {
return testRequest
}
init(testRequest: URLRequest) {
self.testRequest = testRequest
super.init()
}
}
// setup
var receivedPolicy: WKNavigationActionPolicy?
let fakeAction = FakeNavigationAction(testRequest: ...)
// act
delegateObject.webView(webView, decidePolicyFor: fakeAction, decisionHandler: {
receivedPolicy = $0
})
XCTAssertEqual(receivedPolicy, theExpectedValue)
Upvotes: 1