Unit testing completion handler

When unit testing asynchronous code like a completion handler, we could end up with false positives in case we just add some assert in the completion block. A better strategy is shown in the following sample. We will eliminate the false positive by the use of the waitForExpectations function.

class AsyncUnitTests: XCTestCase {
    func testGivenValidConnectionShouldReturnThatFeatureFlagIsDisabled() {
        let asyncMock = MockAsyncStuff()
        let fetchFeatureFlagsExpectation = expectation(description: "fetchFeatureFlags")
        asyncMock.fetchFlagsWithCompletionHandler { (isDisabled) in
            fetchFeatureFlagsExpectation.fulfill()
            XCTAssertTrue(isDisabled)
        }
        waitForExpectations(timeout: 5, handler: nil)
    }
}