title: Unit test case view controller crash swift
body: <p>I am writing unit test cases for the view controllers in my iOS app. I am trying to test if the UI elements involving IBOutlets are not nil as in the code below.</p>
  
  <pre><code>class ClientsViewControllerTests: XCTestCase {
  
  var clientsVC: ClientsTableViewController?
  
  override func setUp() {
      super.setUp()
  
      let storyboard = UIStoryboard(name: "Clients", bundle: nil)
  
      clientsVC = storyboard.instantiateInitialViewController() as? ClientsTableViewController
  
      if let vc = clientsVC?{
          vc.loadView()
      }
  }
  
  override func tearDown() {
  
      super.tearDown()
      clientsVC = nil
  }
  
  func testClientViewControllerNotNil(){
  
      XCTAssertNotNil(clientsVC, "view controller cannot be nil")
  }
  </code></pre>
  
  <p>I test fails and outputs <code>"view controller cannot be nil"</code></p>
  
  <p>I fail to understand why.However, the below test passes:</p>
  
  <pre><code>func testStoryBoard(){
      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      var vc = storyboard.instantiateViewControllerWithIdentifier("MainVC") as UIViewController
      XCTAssertNotNil(vc,"Storyboard is not connected with a viewcontroller")
  </code></pre>
  
  <p>But I need to do it in the 1st method as I want to test for the IBOutlet bindings for the Specific viewController like:</p>
  
  <pre><code>XCAssertNotNil(vc.sendButton, "Send button is nil")
  </code></pre>
  
  <p>Kindly guide me why the test is failing and also how to test outlet binding and the action binding in ViewControllers</p>