gpt4 book ai didi

ios - 如何对特定于语言环境的本地通知进行单元测试

转载 作者:IT王子 更新时间:2023-10-29 05:53:32 24 4
gpt4 key购买 nike

我刚刚将本地通知添加到我的应用程序。如果应用程序区域设置的 regionCode(即 Locale.current.regionCode)是“US”或“CA”,这些通知应该触发。我对语言环境的语言感兴趣。

我也会想编写补充测试用例,但是一旦我知道如何编写一个测试用例,另一个就会自然而然。

因此,我的问题是:如何将 Locale 注入(inject)到测试中(参见 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()

最佳答案

如果您编辑您的方案并选择测试/选项,您可以设置应用程序区域:
enter image description here有不同的手动测试方案就足够了吗?

此外,如果您创建了一个机器人,您可以设置构建配置,以便它选择某个区域:enter image description here如果您定义不同的机器人,您可以自动运行测试。

关于ios - 如何对特定于语言环境的本地通知进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56275400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com