单独运行成功的单元测试,但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)")
}
})
}
How to compute decibel (dB) of Amplitude from Media Player?
我也能用更简单的代码重现这个:
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}
其他一些答案表明这可能是由系统警报引起的。这是可以理解的,我正在使用需要权限警报的位置服务。但是,我运行测试的设备已经接受了权限,因此不应显示警报。
答案 0 :(得分:0)
我遇到了类似的问题-一组多个expectations
崩溃的测试-遇到了您的问题。它使我意识到,它的权限导致了问题-这里是位置管理器,对我来说是语音识别。因此,我嘲笑了我的授权请求类并注入了肯定的响应。
您必须去搜索那些需要权限的方法,无论它们是否使用expectations
。