Bar*_*tal 8 locale ios localnotification swift unusernotificationcenter
我刚刚在我的应用中添加了本地通知。仅当应用区域设置regionCode(即Locale.current.regionCode)为“ US”或“ CA”时,才触发这些通知。我对语言环境不感兴趣language。
我还将要编写补充的测试用例,但是一旦我知道如何编写一个测试用例,其他的就自然而然地遵循了。
因此,我的问题是:如何将Locale注入测试中(请参阅参考资料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)
}
}
Run Code Online (Sandbox Code Playgroud)
假设默认setup()和tearDown()。
小智 1
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 testSuccessfulCanadaNotificationDelivery() {
let canadaLocale = Locale("en_CA")
XCTAssertTrue(notificationDelivered(with: canadaLocale))
}
func testNotificationDeliveryFailure() {
let notCanadaOrUs = Locale("ru_RU")
XCTAssertFalse(notificationDelivered(with: notCanadaOrUs))
}
private func notificationDelivered(with locale: Locale) -> Bool {
// The answer to my question would go here.
// (Inject Locale into the test, somehow?)
notificationManager.schedule(notifications: [notification1, notification2],
withRegionCode: locale.regionCode)
let expectation = self.expectation(description: "notification delivery")
var deliveredNotifications: [UNNotification]?
UNUserNotificationCenter.current().getDeliveredNotifications {
deliveredNotifications = $0
expectation.fulfill()
}
waitForExpectations(timeout: 10, handler: nil)
return (deliveredNotifications?.count ?? 0) == 2
}
Run Code Online (Sandbox Code Playgroud)
你能做这样的事情吗?
| 归档时间: |
|
| 查看次数: |
311 次 |
| 最近记录: |