英文:
Cannot set location of an XCUIDevice to nil
问题
我的UI测试要求CLLocationManager
实例的location
属性最初为nil
。
因此,在UI测试的方案中,运行选项将默认位置设置为none
。
UI测试使用:
class IPhone_UITestCase: XCTestCase {
//…
let device = XCUIDevice.shared
//…
override func setUp() {
super.setUp()
device.location = nil
//…
myApp.launch()
//…
}
}
而应用程序使用一个单例:
final class LocationManager: CLLocationManager {
static let shared = LocationManager()
//…
}
我期望在我的应用程序启动后,LocationManager.shared.location == nil
。
然而,应用程序以location != nil
启动。
可能的原因是什么?
编辑:
当在将模拟设备位置设置为nil
后在UI测试中停在断点时,有以下情况:
Xcode控制台显示UI测试应用程序为Resetting the simulated location
,这是预期的。
但是对于应用程序,Xcode控制台显示CLLocationManager
在其代理中调用了locationManager(_:didFailWithError:)
,错误为The operation couldn’t be completed. (kCLErrorDomain error 0.)
,并且模拟设备位置未重置。
英文:
My UI tests require that property location
of a CLLocationManager
instance is initially nil
.
Thus, in the scheme for the UI tests, the run options set the default location to none
.
The UI tests use
class IPhone_UITestCase: XCTestCase {
//…
let device = XCUIDevice.shared
//…
override func setUp() {
super.setUp()
device.location = nil
//…
myApp.launch()
//…
}
and the app uses a singleton
final class LocationManager: CLLocationManager {
static let shared = LocationManager()
//…
}
I expected that in my app after launch, LocationManager.shared.location == nil
.
However, the app is launched with location != nil
.
What could be the reason?
EDIT:
When the UI test is stopped at a breakpoint after setting the simulated device location to nil
, one has the following situation:
The Xcode console shows for the UI test app Resetting the simulated location
, as expected.
But for the app, the Xcode console shows that the CLLocationManager
did call locationManager(_:didFailWithError:)
in its delegate, with the error The operation couldn’t be completed. (kCLErrorDomain error 0.)
and the simulated device location was not reset.
答案1
得分: 0
我联系了苹果(TSI)并得到了答案
>...我们认为您的问题在Xcode文档中已经得到了回答
>链接如下:
>构建单元测试的位置
>https://developer.apple.com/documentation/xcode/simulating-location-in-tests#Construct-locations-for-unit-tests
文档表示:
>...通过将共享的XCUIDevice位置设置为XCUILocation的实例来更新模拟设备位置。
显然,这需要解释为不允许(即使可能)将location
属性设置为nil
,因为nil
不是XCUILocation
的实例。
英文:
I contacted Apple (TSI) and got the answer
> … we believe your question is answered by the Xcode documentation
> linked here:
>
Construct locations for unit tests
> https://developer.apple.com/documentation/xcode/simulating-location-in-tests#Construct-locations-for-unit-tests
The docu says:
> ... update the simulated device location by setting the shared
> XCUIDevice location to an instance of XCUILocation.
Apparently this has to be interpreted that it is not allowed (even if possible) to set the location
property to nil
, since nil is not an XCUILocation
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论