I have just added local notifications to my app. These notifications are supposed to fire only if the app Locale's regionCode
(i.e. Locale.current.regionCode
) is "US" or "CA". I am not interested in the locale's language
.
I will also want to write the complementary test case, but once I know how to write one test case, the other will follow naturally.
Therefore, my question is: How can a Locale be injected into the test (see testSuccessfulNotificationDelivery()
)?
LocalNotificationTests.swift:
class LocalNotificationTests: XCTestCase {
let notification1 = LocalNotification(toTriggerInSeconds: 5)
let notification2 = LocalNotification(toTriggerInSeconds: 6)
// This object manages LocalNotifications
// by building them into UNUserNotifications
// and then scheduling them using UNUserNotificationCenter.
let notificationManager = NotificationManager()
func testSuccessfulNotificationDelivery() {
// setup locale and use it for testing, somehow
let π¨π¦ = Locale(identifier: "en_CA")
// The answer to my question would go here.
// (Inject Locale into the test, somehow?)
notificationManager.schedule(notifications: [notification1, notification2],
withRegionCode: π¨π¦.regionCode)
let expectation = self.expectation(description: "notification delivery")
var deliveredNotifications: [UNNotification]?
UNUserNotificationCenter.current().getDeliveredNotifications {
deliveredNotifications = $0
expectation.fulfill()
}
waitForExpectations(timeout: 10, handler: nil)
XCTAssertEqual(deliveredNotifications?.count, 2)
}
}
Assume default setup()
and tearDown()
.
NotificationManager
check the Locale and schedules the notifications only if it isen_US
? β CwNotificationManager
has a method calledschedule()
that checksLocale.current.regionCode
. It also has another version ofschedule()
where I can pass in theregionCode
as a parameter and it will use that region to decide whether to schedule or not. I will edit my question to reflect this! β Alitaschedule()
with the correct Locale and verify that the notification is sent, would that be satisfactory? I don't exactly understand the problem.. β CwLocale.current.regionCode
ofUS
. What if I want to test that the manager works correctly with aregionCode
set toCA
, WITHOUT manually setting the region of the simulator to Canada every single time I want to test my code? β Alita