How to unit test a static property?

Viewed 1931

I have a ConferenceNumberDirectory that has a build function which parses a JSON file to create static Arrays of ConferenceNumber objects. I want to unit test this function.

I created a unit test using a smaller JSON file to test this but my tests are failing as the test values are simply being appended on to the actual values. My real values amount to 56 entries for the ConferenceNumberDirectory.att array, but when I run my tests these values are still here and my test values (13 entries) are appended on to the original array. How do I test my static properties without poisoning my them with the test data? In previous commits my unit test ConferenceNumberDirectory.att property and the actual one were separate entities, I don't know what's happening here (I have not changed my test class since this commit).

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)
    }

}
1 Answers
Related