【问题标题】:Unit testing UILocalNotification单元测试 UILocalNotification
【发布时间】:2015-10-11 19:46:22
【问题描述】:

我正在编写一个包装UILocalNotifiations 的框架,并且正在为其编写单元测试。它实际上更像是一个功能测试,但无论如何。

所以,我要写的测试是:

func testAlertManagerCanRegisterLocalNotifications() {

    let manager = Manager()
    manager.alerts = DummyAlerts

    let app = UIApplication.sharedApplication()
    let scheduledNotifs = app.scheduledLocalNotifications ?? []
    XCTAssertEqual(scheduledNotifs.count, manager.alerts.count)
}

鉴于当 alerts 属性发生突变时管理器同步调度 UILocalNotifications,此测试应该通过.. 但它没有。

这里的根本原因是测试没有必要的权限来安排通知,我不知道我应该如何允许测试应用程序获得这些权限?毕竟,没有涉及 UI。

问题:

是否可以强制测试应用拥有必要的UIUserNotification 权限?

【问题讨论】:

  • 当然,一种解决方案是模拟UIApplication 对象,但试图找到更好的方法。

标签: ios unit-testing uilocalnotification xctest uiapplication


【解决方案1】:

您现在可能已经弄清楚了,但是如果您将虚拟警报设置为立即触发,它不会显示在您的 scheduledNotifs 中。相反,如果您延迟警报,它们将在那里。

【讨论】:

  • 不,它们已正确设置为将来的日期。问题在于权限,如问题中所述......我还没有弄清楚:(
  • 我能够通过先测试注册通知,然后测试创建通知来开始工作
【解决方案2】:

我能够通过先进行注册通知的测试,然后进行创建通知的测试来开始工作

func testNotificationRegister() {
    manager.registerForUserNotifications()

    let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
    XCTAssertTrue(settings != .None)
}

func testCreateNotification()
{
    let conf = common.createMockConference()
    manager.createNotifcationFromConference(conf, date: NSDate().dateByAddingTimeInterval(30))
    XCTAssertTrue(UIApplication.sharedApplication().scheduledLocalNotifications?.count > 0)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2011-04-12
    • 2023-03-21
    相关资源
    最近更新 更多