相关疑难解决方法(0)

单元测试iOS 10通知

在我的应用程序中,我希望断言已以正确的格式添加通知.我通常会使用依赖注入来执行此操作,但我无法想到测试新UNUserNotificationCenterAPI的方法.

我开始创建一个模拟对象来捕获通知请求:

import Foundation
import UserNotifications

class NotificationCenterMock: UNUserNotificationCenter {
    var request: UNNotificationRequest? = nil
    override func add(_ request: UNNotificationRequest, withCompletionHandler completionHandler: ((Error?) -> Void)? = nil) {
        self.request = request
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,UNUserNotificationCenter没有可访问的初始化程序,我无法实例化模拟.

我甚至不确定我是否可以通过添加通知请求和获取当前通知来进行测试,因为测试需要在模拟器上请求权限以阻止测试.目前我已经将通知逻辑重构为包装器,所以我至少可以在整个应用程序中模拟它并手动测试.

我有比手动测试更好的选择吗?

notifications unit-testing ios ios10

12
推荐指数
1
解决办法
2482
查看次数

标签 统计

ios ×1

ios10 ×1

notifications ×1

unit-testing ×1