I am using test-driven development on Xcode and in Swift to develop an app.
I want to add timed notification alerts to remind people to come back to the app to perform an action. To do that I need to request authorization from my user using the notification center.
To do this, I want to write a test in my unit tests that only passes when the shared UNUserNotificationCenter instance calls its requestAuthorization(options:completionHandler:) method.
I have tried mocking UNUserNotificationCenter:
extension NotificationsExperimentsTests {
class MockNotificationCentre: UNUserNotificationCenter {
var didRequestAuthorization = false
override func requestAuthorization(options: UNAuthorizationOptions = [], completionHandler: @escaping (Bool, Error?) -> Void) {
didRequestAuthorization = true
}
}
}
But then when I try and initialise it in a test,
func test_requestAuthorization_IsCalled() {
var mockNotificationCenter = MockNotificationCentre()
}
the compiler tells me that:
'NotificationsExperimentsTests.MockNotificationCentre' cannot be constructed because it has no accessible initializers
I'm not sure what to try next, or even whether what I'm trying to do is possible?