1

I am writing a framework that wraps UILocalNotifiations, and in the processes of writing unit tests for it. It's actually more of a functional test, but whatever.

So, the test I am trying to write is:

func testAlertManagerCanRegisterLocalNotifications() {

    let manager = Manager()
    manager.alerts = DummyAlerts

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

Given that the manager synchronously schedules UILocalNotifications when the alerts property is mutated, this test should pass .. but it doesn't.

The root cause here is that the tests don't have the necessary permissions to schedule notifications, and I have no idea how am I suppose to allow the test app these permissions? After all, there is no UI involved.

The Question:

Is it possible to force the test app to have the necessary UIUserNotification permissions?

Mazyod
  • 22,319
  • 10
  • 92
  • 157
  • Of course, one solution is to mock the `UIApplication` object, but trying to find a better way. – Mazyod Oct 11 '15 at 19:58

2 Answers2

1

You probably figured it out by now, but if you have your dummy alerts set to fire immediately it will not show in your scheduledNotifs. If instead you delay the alerts they will be in there.

Jonathan
  • 614
  • 1
  • 9
  • 18
  • No, they are set properly to a date in the future. The problem is with permissions, as mentioned in the question... I haven't figured it out yet :( – Mazyod Nov 12 '15 at 04:34
  • I was able to get to work by having a test to register notifications first, then a test to create a notification – Tony Apr 01 '16 at 19:25
0

I was able to get to work by having a test to register notifications first, then a test to create a notification

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)
}
Tony
  • 656
  • 8
  • 20