title: How Can I Unit Test Calling of iOS Application Delegate Methods?
body: <p>I have an iOS application that integrates with the GitHub API. I am unit testing my <code>OAuth</code> requests, which requires testing the receipt of a code from the GitHub API that I will use to exchange for a token.</p>
  
  <p>In my <code>AppDelegate.swift</code>, I have the following method, which is used to handle the callback from GitHub when the user authorizes my application to use their GitHub account:</p>
  
  <pre class="lang-swift prettyprint-override"><code>func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -&gt; Bool {
      return true
  }
  </code></pre>
  
  <p>The steps are as follows:</p>
  
  <ol>
  <li>Open the application.</li>
  <li>Using the URL for authorizing GitHub account access (<a href="https://github.com/login/oauth/authorize" rel="nofollow noreferrer">https://github.com/login/oauth/authorize</a>), an instance of <code>SFSafariViewController</code> is presented, allowing the user to press the 'Authorize' button.</li>
  <li>GitHub uses the callback URL to my application that I provided when registering my application with GitHub, which sends a notification to open my app.</li>
  <li>The method above is executed, where I retrieve the <code>code</code> parameter from <code>url</code>.</li>
  </ol>
  
  <p>However, I am stuck trying to find a way to test this without actually making a request to the GitHub API. I can create a <code>URL</code> instance that mimics what GitHub supplies my application with, but I would like to test this without making an actual request. </p>
  
  <p>Is there a way to unit test this, or is this something that I shouldn't worry about since it's handled by the OS, and instead, only test my code for parsing the <code>code</code> parameter of a test <code>URL</code>?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>After taking Jon's <a href="https://stackoverflow.com/questions/41667280/how-can-i-unit-test-calling-of-ios-application-delegate-methods#41668489">advice</a>, I created a test class to allow me to simulate the GitHub callback in action:</p>
  
  <pre class="lang-swift prettyprint-override"><code>class GitHubAuthorizationCallbackTests: XCTestCase {
  
      let delegate = AppDelegateMock()
  
      func test_AuthorizationCallbackFromGitHub_ApplicationOpensURL() {
          guard let url = URL(string: "xxxxxxxxxxxxxx://?code=********************") else { return XCTFail("Could not construct URL") }
          let isURLOpened = delegate.application(UIApplication.shared, open: url)
          XCTAssertTrue(isURLOpened, "URL is not opened from GitHub authorization callback. Expected URL to be opened from GitHub authorization callback.")
      }
  
  }
  </code></pre>
  
  <p>Then, I created <code>AppDelegateMock.swift</code> to be used instead of <code>AppDelegate.swift</code>, adding in the intended method to be called when the GitHub callback is executed to open my app:</p>
  
  <pre class="lang-swift prettyprint-override"><code>import UIKit
  
  class AppDelegateMock: NSObject, UIApplicationDelegate {
  
      func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -&gt; Bool {
          return true
      }
  
  }
  </code></pre>
  
  <p>The test passes, allowing me to test the logic that I need to test for handling the <code>code</code> parameter that is returned from GitHub int he <code>url</code> parameter of the method.</p>