Swift 5 中 UNUserNotificationCenter .requestAuthorization 的单元测试

Unit testing for UNUserNotificationCenter .requestAuthorization in Swift 5

我在 Xcode 和 Swift 上使用测试驱动开发来开发应用程序。

我想添加定时通知提醒,提醒人们返回应用程序执行操作。为此,我需要使用通知中心向我的用户请求授权。

为此,我想在我的单元测试中编写一个测试,该测试仅在共享的 UNUserNotificationCenter 实例调用其 requestAuthorization(options:completionHandler:) 方法时通过。

我试过模拟 UNUserNotificationCenter:

extension NotificationsExperimentsTests {

    class MockNotificationCentre: UNUserNotificationCenter {

        var didRequestAuthorization = false

        override func requestAuthorization(options: UNAuthorizationOptions = [], completionHandler: @escaping (Bool, Error?) -> Void) {
            didRequestAuthorization = true
        }
    }
}

但是当我尝试在测试中初始化它时,

func test_requestAuthorization_IsCalled() {

    var mockNotificationCenter = MockNotificationCentre()
}

编译器告诉我:

'NotificationsExperimentsTests.MockNotificationCentre' cannot be constructed because it has no accessible initializers

我不确定接下来要尝试什么,甚至不确定我正在尝试做的事情是否可行?

这就是我目前所处的位置。感谢之前的回答:

我的测试class:

import XCTest
import UserNotifications

@testable import NotificationsExperiments

class TestClass: XCTestCase {

    override func setUp() {
     super.setUp()
  }

  func testDoSomething() {
        //Given
        // Class being tested
        let exampleClass = ExampleClass()
        // Create your mock class.
        let mockNotificationCenter = MockNotificationCenter()
        exampleClass.notificationCenter = mockNotificationCenter
    //When
        exampleClass.doSomething()
        //Then
        XCTAssertTrue(mockNotificationCenter.didRequestAuthorization)
  }
}


extension TestClass {

    class MockNotificationCenter: MockUserNotificationCenterProtocol {

        var didRequestAuthorization = false

        func requestAuthorization(options: UNAuthorizationOptions, completionHandler: ((Bool, Error?) -> Void)) {
            didRequestAuthorization = true
        }
    }
}

我的例子class:

import Foundation
import UserNotifications

class ExampleClass {

    #if DEBUG
    var notificationCenter: MockUserNotificationCenterProtocol = UNUserNotificationCenter.current()
    #else
    var notificationCenter = UNUserNotificationCenter.current()
    #endif

    func doSomething() {
        let options: UNAuthorizationOptions = [.alert, .sound, .badge]
        notificationCenter.requestAuthorization(options) {
            (didAllow, error) in
            if !didAllow {
                print("User has declined notifications")
            }
        }
  }
}

#if DEBUG
protocol MockUserNotificationCenterProtocol: class {
    func requestAuthorization(options: UNAuthorizationOptions, completionHandler: ((Bool, Error?) -> Void))

}

extension UNUserNotificationCenter: MockUserNotificationCenterProtocol {
    func requestAuthorization(options: UNAuthorizationOptions, completionHandler: ((Bool, Error?) -> Void)) {
        print("Requested Authorization")
    }
}
#endif

这确实有效,但它不仅仅是 的 hacky。在 DEBUG 模式下,它实际上不会发送授权请求,但会在发布时发送。

我们很乐意接受任何进一步的贡献。