Search code examples
xctestxctestexpectation

Using XCTest, how can one chain together multiple discrete sequences of { expectations -> wait }?


The documentation for XCTest waitForExpectationsWithTimeout:handler:, states that

Only one -waitForExpectationsWithTimeout:handler: can be active at any given time, but multiple discrete sequences of { expectations -> wait } can be chained together.

However, I have no idea how to implement this, nor can I find any examples. I'm working on a class that first needs to find all available serial ports, pick the correct port and then connect to the device attached to that port. So, I'm working with at least two expectations, XCTestExpectation *expectationAllAvailablePorts and *expectationConnectedToDevice. How would I chain those two?


Solution

  • swift

    let expectation1 = //your definition
    let expectation2 = //your definition
    
    let result = XCTWaiter().wait(for: [expectation1, expectation2], timeout: 10, enforceOrder: true)
    
    if result == .completed {
        //all expectations completed in order
    }