قالب وردپرس درنا توس
Home / IOS Development / IOS – How to test local specific UNUserNotifications

IOS – How to test local specific UNUserNotifications



I just added local messages to my app. These alerts should only be burned whose app Locale's regionCode (ie Locale.current.regionCode ) is "US" or "CA". I am not interested in the language language .

My Questions:

1. How can a local be injected into the test (see testSuccessfulNotificationDelivery () )?

2. If we take the premises out of the test package, the test only works partially. The claim fails and says that only 1 message has been delivered instead of 2. I have a feeling that this has to do with my asynchronous character. Can you help me identify the guilty and provide an appropriate solution?

LocalNotificationTests.swift :

  import XCTest
import user notifications
@Testable Import App

class LocalNotificationTests: XCTestCase {

var notification1
: LocalNotification! var notification2: LocalNotification! var notificationManager: NotificationManager! override func setUp () { super.setUp () // LocalNotification is a custom model for local alerts notification1 = LocalNotification (toTriggerInSeconds: 5) notification2 = LocalNotification (toTriggerInSeconds: 6) // This object handles LocalNotifications // by building them into UNUserNotifications // and then schedule them using UNUserNotificationCenter. notificationManager = NotificationManager () } override func tearDown () { super.tearDown () } func testSuccessfulNotificationDelivery () () { // setup locale and use it for testing, somehow let 🇺🇸 = Local (identifier: "en_US") // The answer to (1) would go here. (Inject the Locale into the test, somehow?) notificationManager.schedule (notification1, notification2) let expectation = self.expectation (description: "successful warning delivery") was deliveredNotifications: [UNNotification]? // the answer to (2) is related to this asynchronous code UNUserNotificationCenter.current (). GetDeliveredNotifications { suppliesNotifications = $ 0 expectation.fulfill () } waitForExpectations (timeout: 10, trades: null) // Fail. Only 1 message has been delivered. XCTAssertEqual (suppliesNotifications? .Count, 2) } }

The following assumption should be made:

  • notificationManager works as expected. Outside of this test package I have seen that notificationManager can schedule and deliver as many messages as I want.

Source link