title: How to test if a UIControlEvents has been fired
body: <p>I have a library implementing a custom <code>UIControl</code> with a method which would fire a <code>.valueChanged</code> event when called. I would like to test the method for that behavior.</p>
  
  <p><strong>My custom control:</strong></p>
  
  <pre><code>class MyControl: UIControl {
      func fire() {
          sendActions(for: .valueChanged)
      }
  }
  </code></pre>
  
  <p>And the test:</p>
  
  <pre><code>import XCTest
  
  class ControlEventObserver: NSObject {
      var expectation: XCTestExpectation!
  
      init(expectation anExpectation: XCTestExpectation) {
          expectation = anExpectation
      }
  
      func observe() {
          expectation.fulfill()
      }
  }
  
  class Tests: XCTestCase {
      func test() {
          let myExpectation = expectation(description: "event fired")
          let observer = ControlEventObserver(expectation: myExpectation)
          let control = MyControl()
          control.addTarget(observer, action: #selector(ControlEventObserver.observe), for: .valueChanged)
          control.fire()
          waitForExpectations(timeout: 1) { error in
              XCTAssertNil(error)
          }
      }
  }
  </code></pre>
  
  <p>The problem is the <code>observe</code> method never gets called so the <code>expectation</code> is not fulfilled.</p>
  
  <p>The question is: how can we test for <code>UIControlEvents</code> like in this case? Perhaps we need to force the runloop somehow?</p>
  
  <p>EDIT 1:
  Please note that since I am testing a library, my test target does not have any Host Application. The test above passes when the test target has a host application.</p>