Asynchronous unit testing of unreliable 3rd party API

@jessycatterwaul hate to tag you again but having gone through the set of unit testing videos, I am unsure if the below has a big glaring apparent blunder

func testFetchRoutingFile() {
    // given
    let routingFile = BMRoutingFile(regionName: "testFetchRoutingFile",
                                    bounds: MGLCoordinateBounds(sw: CLLocationCoordinate2D(latitude: 48.20815966713252,
                                                                                             longitude: 16.364719858842847),
                                                                 ne: CLLocationCoordinate2D(latitude: 48.21552619909402,
                                                                                            longitude: 16.376760716321353)),
                                    polygon: nil,
                                    routeId: nil,
                                    offlineRoutingFileId: nil,
                                    offlineRegionId: nil)
    
    // when
    let fileRequestor = BMRoutingFileRequestor(routingFile)
    
    // this call uses external libraries and doesn't use URLSession directly and there is a LOT going on in that chunk and I wonder how I would go about calling `expectation.fulfill()` inside a completion block 
    fileRequestor.execute()
    
    // then
    XCTAssertNotNil(routingFile.offlineRoutingFileId)
    
    expectation.fulfill()
    waitForExpectations(timeout: timeout)
  }

My recommendation is to combine XCTWaiter and Combine. It will take some work to learn it and get some utility extensions set up, but the effort will pay off for you.

More details on XCTWaiter here: https://developer.apple.com/wwdc17/409
And integration, here: https://vojtastavik.com/2019/12/11/combine-publisher-blocking-recorder/

If I’m the one tackling the next course update, I’ll put this in, but that update won’t be happening this year. Good luck, and let me know if you get stuck. You can always just use someone else’s implementation, too! e.g. https://github.com/industrialbinaries/CombineTestExtensions

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.