我在編寫UI測試時遇到了類似的問題,因爲模擬器/繫留設備無法做到您想要的任何事情。我所做的就是寫出模仿所需行爲的模擬(我通常無法控制的東西)。
替換CLLocationManager的自定義位置管理器將允許您完全控制位置更新,因爲您可以通過CLLocationManagerDelegate方法以編程方式發送位置更新:locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
。
創建一個類MyLocationManager
,使其成爲CLLocationManager
的子類,並讓它覆蓋您調用的所有方法。不要在重寫的方法中調用super,因爲CLLocationManager應該永遠不會實際接收方法調用。
class MyLocationManager: CLLocationManager {
override func requestWhenInUseAuthorization() {
// Do nothing.
}
override func startUpdatingLocation() {
// Begin location updates. You can use a timer to regularly send the didUpdateLocations method to the delegate, cycling through an array of type CLLocation.
}
// Override all other methods used.
}
的delegate
屬性不會需要重寫(而且不能),但你可以訪問它作爲CLLocationManager的子類。
要使用MyLocationManager
您應該傳遞啓動參數,告訴您的應用程序它是否是UITest。在你的測試用例的方法setUp
插入這行代碼:
app.launchArguments.append("is_ui_testing")
商店CLLocationManager因爲這是一個MyLocationManager測試時的屬性。當不測試CLLocationManager將被用作正常。
static var locationManger: CLLocationManager = ProcessInfo.processInfo.arguments.contains("is_ui_testing") ? MyLocationManager() : CLLocationManager()
這是嚴重不可能的嗎? 所以我最好的選擇是使用時間戳GPX文件並在每次位置更改時都截圖。 – Nbfour