This is the context:
I'm implementing Unit Tests into an internal Swift Package. A method into one Unit Test is trying to get Notification Center reference from UNUserNotificationCenter.current()
and it is crashing returning this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'bundleProxyForCurrentProcess is nil: mainBundle.bundleURL file:///Applications/Xcode%2014.3.1.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents/'.
I already made
Possible solution 1: Create a mock NotificationCenter from a protocol, which is used to extend UNUserNotificationCenter. It doesn't work, because when I want to access to method current() from variable userNotificationCenter, is not possible. Here the example.
import UserNotifications
protocol UserNotificationCenterProtocol {
func requestAuthorization(options: UNAuthorizationOptions, completionHandler: @escaping (Bool, Error?) -> Void)
}
extension UNUserNotificationCenter: UserNotificationCenterProtocol {}
class NotificationCenterManager {
private let userNotificationCenter: UserNotificationCenterProtocol
init(userNotificationCenter: UserNotificationCenterProtocol = UNUserNotificationCenter.current()) {
self.userNotificationCenter = userNotificationCenter
}
}
class MockUserNotificationCenter: UserNotificationCenterProtocol {
func requestAuthorization(options: UNAuthorizationOptions, completionHandler: @escaping (Bool, Error?) -> Void) {
}
}
Possible solution 2: Implementing UNUserNotificationCenterDelegate's method into a class, then pass NotificationCenterDelegate to this class, but also is not possible:
class NotificationCenterMock: NSObject, UNUserNotificationCenterDelegate {
// Some code
}
let notificationCenterMock = NotificationCenterMock()
let UNUserNotificationCenter.current().delegate = notificationCenterMock
Anyone has a solution or more ideas? Also help to know if is not possible. My theory is that is not possible to get UNUserNotificationCenter.current() for two reasons:
- It's a Unit Test target and there aren't Notification Center in that target
- Is not possible to get Notification Center is if a Swift Package
That are theories but I haven't found documentation that help me to prove that.
Thank you for your help!