我有一个单元测试,它在单独运行时成功,但在与其他测试一起运行时(大部分时间)崩溃。EXC_BAD_ACCESS
waitForExpectations
func testStartMonitoring() {
let mockLocationManager = MockLocationManager()
let beaconListener = BeaconListener(locationManager: mockLocationManager, uuid: BeaconListenerTests.defaultUUID)
let e = self.expectation(description: "Expected beacons to be detected")
//If the listener calls back, the expectation succeeds.
beaconListener.didReceiveNewRoomInformation = { data in
//There are three entries in the test data
XCTAssert(data.count == 3)
e.fulfill()
}
//Start listening
beaconListener.startListening()
//Wait up to 15s for a response
self.waitForExpectations(timeout: 15.0, handler: {error in
if let error = error {
print("\(error)")
}
})
}
- 我没有其他异步测试
- 测试永远不会因超时而失败
- 由于测试仅在某些时候崩溃,我预计问题是某个地方的竞争条件,但我不确定在哪里看。
我也可以用更简单的代码来重现它:
func testStartMonitoring() {
let e = self.expectation(description: "Expected beacons to be detected")
let deadlineTime = DispatchTime.now() + .seconds(1)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
e.fulfill()
}
//Wait up to 15s for a response
self.waitForExpectations(timeout: 15.0, handler: {error in
})
}
我从命令行运行了测试,发现了这条额外的信息:
Error Domain=IDETestOperationsObserverErrorDomain Code=5 "Early unexpected exit, operation never finished bootstrapping - no restart will be attempted" UserInfo={NSLocalizedDescription=Early unexpected exit, operation never finished bootstrapping - no restart will be attempted}
其他一些答案表明这可能是由系统警报引起的。这是可以理解的,我正在使用需要权限警报的位置服务。但是,我正在运行测试的设备已经接受了权限,因此不应该显示警报。