title: UIView.animateWithDuration:animations:completion: gets canceled in XCTest
body: <p>I'm trying to unit test an extension on <code>UIView</code>, which calls <code>animateWithDuration:animations:completion:</code>.</p>
  
  <p>Unfortunately all animations in the test case gets canceled immediately and therefore the <code>isFinished</code> parameter of the completion block is always <code>false</code>.</p>
  
  <p>Does anyone have an idea how to get animations working in a test case?</p>
  
  <p>This is my playground code:</p>
  
  <pre class="lang-swift prettyprint-override"><code>import UIKit
  import XCTest
  
  extension UIView {
      func fadeOut(duration: TimeInterval, completion: ((Bool) -&gt; Void)?) {
          UIView.animate(withDuration: duration,
                         animations: {
                              self.alpha = 0.0
                         },
                         completion: { isFinished in
                              self.isHidden = isFinished
  
                              completion?(isFinished)
                         })
      }
  }
  
  class UIViewTests: XCTestCase {
  
      func testFadeView() {
          // Given
          let expectation = self.expectation(description: "Expect completion handler to be called.")
  
          let view = UIView()
          view.alpha = 1.0
  
          // When
          print("Date before", Date())
          view.fadeOut(duration: 1.0) { (isFinished) in
              print("Date completed", Date())
              print("isFinished", isFinished)
  
              expectation.fulfill()
          }
  
          // Then
          wait(for: [expectation], timeout: 1.1)
  
          XCTAssertTrue(view.isHidden) // &lt;- this assertion always fails, cause `isFinished` inside the completion handler is always `false`.
          XCTAssertEqual(view.alpha, 0.0, accuracy: CGFloat.ulpOfOne)
      }
  }
  
  UIViewTests.defaultTestSuite.run()
  </code></pre>
  
  <p>The assertion <code>XCTAssertTrue(view.isHidden)</code> always fails. Furthermore the log statements output:</p>
  
  <pre><code>Date before 2019-05-20 23:30:35 +0000
  Date completed 2019-05-20 23:30:35 +0000
  </code></pre>
  
  <p>So basically the animation is killed immediately.</p>