Mocking UIViewController from storyboard
<p>I have a question about mocking a UIViewController in my Unit Tests that is from the storyboard. I have the view controller loaded from storyboard like this in the <code>setUp()</code> method which is fine:</p>

<pre><code>var exampleVC: ExampleViewController!

override func setUp() {
    super.setUp()

    exampleVC = storyboard.instantiateViewControllerWithIdentifier("exampleVC") as! ExampleViewController
    UIApplication.sharedApplication().keyWindow?.rootViewController = exampleVC
    let _ = exampleVC.view
}
</code></pre>

<p>However, the issue is how can I then mock/override methods in <code>exampleVC</code>. I have tried to instead create a ExampleViewController subclass and create the mock class in the test method like this:</p>

<pre><code>func testExampleMethod() {
    class ExampleViewControllerMock: ExampleViewController {
        var testMethodWasCalled: Bool = false

        override func testMethod() {
            testMethodWasCalled = true
        }
    }

    let exampleVCMock = ExampleViewControllerMock()
    exampleVCMock.testMethod()

    XCTAssertTrue(exampleVCMock.testMethodWasCalled)
}
</code></pre>

<p>This approach crashes on the test as the view and other IBOutlets are nil on <code>exampleVCMock</code> and don't get loaded when the view controller is instantiated this way (so the exampleVC needs to be instantiated from storyboard).</p>

<p>So the question is how I can instantiate the exampleVC from storyboard (so the outlets are connected correctly) but at the same time be able to override methods/create the mock correctly for my unit tests?</p>

<p>Any advice appreciated thanks.</p>