我正在尝试为正在开发的某些模型对象编写一些单元测试。从字典[String:Any]中获取时间戳时,我看到一些奇怪的意外行为。尝试强制转换为非可选时,时间戳为nil。但是您可以在打印输出中看到它是一个时间戳。有人有什么想法吗?
两个打印语句的输出为:
nil
Optional(<FIRTimestamp: seconds=1562802592 nanoseconds=1123>)
import XCTest
import Firebase
@testable import MyTarget
class UserTest: XCTestCase {
var testDict:[String : Any]? = [:]
var renewalDate:Timestamp = Timestamp(seconds: Int64(1562802592), nanoseconds: Int32(1123))
override func setUp() {
super.setUp()
testDict = [
"userId": "userId",
"firstName": "J",
"lastName" : "R",
"email": "j@example.com",
"renewalDate": renewalDate
]
}
func testInit_ReturnsInstanceWhenRequiredPropertiesAreSpecificed() {
let zUser = ZenUser(dictionary: testDict)
XCTAssertNotNil(zenUser, "ZenUser should not be nil")
}
}
ZenUser : DocumentDeserializable {
init?(dictionary:[String:Any]?) {
let t = dictionary?["renewalDate"] as? Timestamp
print(t)
print(dictionary?["renewalDate"])
guard let userId = dictionary?["userId"] as? String,
let firstName = dictionary?["firstName"] as? String,
let renewalDate = dictionary?["renewalDate"] as? Timestamp,
let lastName = dictionary?["lastName"] as? String else {
print("Missing fields, returning nil")
return nil
}
...
}
protocol DocumentDeserializable {
init?(dictionary: [String: Any]?)
}
为什么强制转换为非可选时时间戳为零?
这是我正在使用的版本。
Using Firebase (6.4.0)
Using FirebaseABTesting (3.0.0)
Using FirebaseAnalytics (6.0.3)
Using FirebaseAuth (6.2.0)
Using FirebaseAuthInterop (1.0.0)
Using FirebaseCore (6.0.4)
Using FirebaseFirestore (1.4.1)