我刚刚将本地通知添加到了我的应用中。如果应用区域设置的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()
。
答案 0 :(得分:0)
它不能完全满足您的需求,但是您可以检查此post。
这似乎是有关更改测试用例中的位置样式的重要说明。
一旦我们在模拟器或设备中执行了测试,它便可以在下一个XCUI测试用例中使用相同的语言和语言环境。无法清除测试用例之间的模拟器内容,因此最好为每个国家/地区和地区创建新方案
您还可以检查Apple Documentation
答案 1 :(得分:0)
答案 2 :(得分:0)
我不确定您是否可以使用“ XCTest”来执行此操作,但是我确定您可以使用“ XCUITest”来执行此操作。您可能可以这样做吗?
class CALocalNotificationTests: LocalNotificationTests {
override func setUp() {
super.setUp()
let app = XCUIApplication()
app.launchArguments = ["-AppleLocale", "en_CA"]
app.launch()
}
func testSuccessfulNotificationDelivery() {
testSuccessfulNotificationDelivery(with: Locale(identifier: "en_CA"))
}
}
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(with locale: Locale) {
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)
XCTAssertEqual(deliveredNotifications?.count, 2)
}
}
我认为这很琐碎,但是app.launchArguments = ["-AppleLocale", "en_CA"]
是关键。
您可以为不同的语言环境创建不同的测试(子)类。
答案 3 :(得分:0)
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
}
你能做这样的事吗?