我有一个
ConferenceNumberDirectory
build
函数,该函数解析JSON文件以创建
ConferenceNumber
物体。我想对这个函数进行单元测试。
我使用一个较小的JSON文件创建了一个单元测试来测试这一点,但我的测试失败了,因为测试值只是附加到实际值上。
ConferenceNumberDirectory.att
数组,但当我运行测试时,这些值仍然在这里,并且我的测试值(13个条目)被附加到原始数组中。如何测试静态属性而不让测试数据影响它们?在之前提交的单元测试中
属性和实际的一个是独立的实体,我不知道这里发生了什么(自从这次提交以来,我没有更改我的测试类)。
class ConferenceNumberDirectory {
static var countryNumbers = [Any]()
static var att = [ConferenceNumber]()
static var arkadin = [ConferenceNumber]()
static var webex = [ConferenceNumber]()
static var zoom = [ConferenceNumber]()
static func build(from rootJSONArray: [Any]?) {
do {
guard let rootJSONArray = rootJSONArray else {
throw SerializationError.missing("JSON Root")
}
for entry in rootJSONArray {
guard let dictionary = entry as? [String: Any] else {
throw SerializationError.missing("JSON Root")
}
guard let isoCode = dictionary["ISO Code"] as? String else {
throw SerializationError.missing("isoCode")
}
guard let country = dictionary["Country"] as? String else {
throw SerializationError.missing("country")
}
if let attTollNumbers = try self.extractNumbers(from: dictionary, forKey: "AT&T toll") {
ConferenceNumberDirectory.att.append(contentsOf: attTollNumbers.map{ConferenceNumber(ISO: isoCode, country: country, number: $0, provider: ConferenceProvider.att, toll: true) })
}
print(ConferenceNumberDirectory.att.count)
if let attTollFreeNumbers = try self.extractNumbers(from: dictionary, forKey: "AT&T toll-free") {
ConferenceNumberDirectory.att.append(contentsOf: attTollFreeNumbers.map{ConferenceNumber(ISO: isoCode, country: country, number: $0, provider: ConferenceProvider.att, toll: false)})
}
if let arkadinTollNumbers = try self.extractNumbers(from: dictionary, forKey: "Arkadin toll") {
ConferenceNumberDirectory.arkadin.append(contentsOf: arkadinTollNumbers.map{ConferenceNumber(ISO: isoCode, country: country, number: $0, provider: ConferenceProvider.arkadin, toll: true) })
}
if let arkadinTollFreeNumbers = try self.extractNumbers(from: dictionary, forKey: "Arkadin toll-free") {
ConferenceNumberDirectory.arkadin.append(contentsOf: arkadinTollFreeNumbers.map{ConferenceNumber(ISO: isoCode, country: country, number: $0, provider: ConferenceProvider.arkadin, toll: false) })
}
if let webexTollNumbers = try self.extractNumbers(from: dictionary, forKey: "Webex toll") {
ConferenceNumberDirectory.webex.append(contentsOf: webexTollNumbers.map{ConferenceNumber(ISO: isoCode, country: country, number: $0, provider: ConferenceProvider.webex, toll: true) })
}
if let webexTollFreeNumbers = try self.extractNumbers(from: dictionary, forKey: "Webex toll-free") {
ConferenceNumberDirectory.webex.append(contentsOf: webexTollFreeNumbers.map{ConferenceNumber(ISO: isoCode, country: country, number: $0, provider: ConferenceProvider.webex, toll: false) })
}
if let zoomTollNumbers = try self.extractNumbers(from: dictionary, forKey: "Zoom toll") {
ConferenceNumberDirectory.zoom.append(contentsOf: zoomTollNumbers.map{ConferenceNumber(ISO: isoCode, country: country, number: $0, provider: ConferenceProvider.zoom, toll: true) })
}
}
}
catch let error {
print(error)
}
}