仍在开发 Xcode 11。swift 5 实现如下所示:
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
//All test by default will use GMT time zone. If different, change it within the func
TimeZone.ReferenceType.default = gmtTimeZone
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
//Rest to current
TimeZone.ReferenceType.default = TimeZone.current
}
在我的 XCTestCase 类中,我的时区声明如下:
private let gmtTimeZone = TimeZone(abbreviation: "GMT")!
private let gmtPlus1TimeZone = TimeZone(abbreviation: "GMT+1")!
private let gmtMinus1TimeZone = TimeZone(abbreviation: "GMT-1")!
我将所有测试都默认为 GMT,但对于特定测试我想更改它,然后:
func test_Given_NotGMTTimeZone_ThenAssertToGMT() {
TimeZone.ReferenceType.default = gmtPlus1TimeZone
...
}
已添加
您还可以使用时区名称,例如“British Summer Time”
private let britishSummerTimeZone = TimeZone(abbreviation: "BST")!
已添加
由于时区缓存,这对我不起作用。我需要在每次更改时区后重置缓存以阻止测试相互干扰。例如
TimeZone.ReferenceType.default = gmtTimeZone
TimeZone.ReferenceType.resetSystemTimeZone ()