title: Unit Test button added to view
body: <p>I am a unit test newbie, I am testing if my button has been added to the view.</p>
  
  <p>My first test passes correctly:</p>
  
  <pre><code>XCTAssertNotNil(vc.button, "button Not Initialised")
  </code></pre>
  
  <p>But my second test fails when i have the view.addSubview(button) in the code</p>
  
  <pre><code>XCTAssertNotNil(vc.button.superview, "button Not added to view")
  </code></pre>
  
  <p>My question is how do i test to see if the button has been added to the view, I am not using storyboards, everything is coded. Because the first test will pass regardless of if i have addSubview in the code or not.</p>
  
  <p>Thanks</p>
  
  <p>In my ViewController class i initialise the button:</p>
  
  <pre><code> private(set) lazy var myButton: UIButton = {
      let myButton = UIButton(frame: CGRectZero)
      return myButton
  }()
  </code></pre>
  
  <p>and in viewDidLoad i add it to subview and give it constraints:</p>
  
  <pre><code>view.addSubview(myButton)
  </code></pre>
  
  <p>My test function is:</p>
  
  <pre><code>func testAddRoomButtonInRoomsViewController() {
      let vc = myViewController()
      XCTAssertNotNil(vc.myButton, "Button Not Initialised")
      XCTAssertNotNil(vc.myButton.superview, "Button Not Added To View")
  }
  </code></pre>