我正在尝试使用默认参数将 CLLocationManager 的创建测试为单音:
+ (GeolocationService *)defaultGeolocationService
{
static GeolocationService *_defaultGeolocationService = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_defaultGeolocationService = [[GeolocationService alloc] init];
[_defaultGeolocationService initLocationManager];
});
return _defaultGeolocationService;
}
- (void)initLocationManager
{
self.locationManager = [CLLocationManager new];
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = YES;
[self.locationManager requestAlwaysAuthorization];
}
Run Code Online (Sandbox Code Playgroud)
测试看起来像这样:
- (void)testInitWithDefaultsSettings
{
GeolocationService *defaultGeolocationService = [GeolocationService defaultGeolocationService];
XCTAssertTrue(defaultGeolocationService.settings.autoPause, @"autoPause");
}
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“非 UI 客户端不能自动暂停”
我应该怎么做才能使这个测试工作?