title: Swift: How do unit test UIWebView loading?
body: <p>My app has a basic webview controller to perform some operations.  This view in the storyboard is not much besides a wrapper around a UIWebView.  The controller itself has various public functions that can be called to load pages in the webview, like so:</p>
  
  <pre><code>class WebViewController: UIViewController, UIWebViewDelegate {
  
    // MARK: Properties
  
    @IBOutlet var webView: UIWebView!
  
    // MARK: UIViewController
  
    override func viewDidLoad() {
      super.viewDidLoad()
      webView.delegate = self
      loadHomePage()
    }
  
    // MARK: Public
  
    public func loadHomePage() {
      navigateWebView(to: HOME_PAGE)
    }
  
    public func loadSettingsPage() {
      navigateWebView(to: SETTINGS_PAGE)
    }
  
    public func loadSignOutPage() {
      navigateWebView(to: SIGN_OUT_PAGE)
    }
  
    // MARK: Private
  
    private func navigateWebView(to url: String) {
      let request = URLRequest(url: URL(string: url)!)
      webView.loadRequest(request)
    }
  </code></pre>
  
  <p>I'm trying to write unit tests that verify that the proper URL is sent to the loadRequest function of the webview.  Note that I don't actually care about loading the URL; this is just a unit test, so all I really want to test is that loadSettingsPage sends a URLRequest with the SETTINGS_PAGE URL to the webview to load, for example.</p>
  
  <p>I tried something like this, with no success:</p>
  
  <pre><code>_ = webViewController.view  // Calls viewDidLoad()   
  XCTAssertEqual(webViewController.webView.request?.url?.absoluteString, HOME_PAGE)
  </code></pre>
  
  <p>The value of the first part of the assertEqual was nil.</p>
  
  <p>I assume I need to mock out the webView somehow but I'm not sure how to go about that.  Any suggestions?</p>
  
  <p>As a follow-up, I'd also like to be able to test when things like <code>webView.reload()</code> and <code>webview.goBack()</code> are called, so any pointers there would be appreciated as well.  Thanks!</p>