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.
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
.