Я только что добавил локальные уведомления в свое приложение.Эти уведомления должны запускаться только , если regionCode
(т. Е. Locale.current.regionCode
) приложения имеет значение "US" или "CA".Я не заинтересован в локали language
.
. Я также хочу написать дополнительный тестовый пример, но как только я знаю, как написать один тестовый пример, другой будет следовать естественным образом..
Следовательно, мой вопрос: Как можно ввести локаль в тест (см. 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)
}
}
Примите значения по умолчанию setup()
и tearDown()
.