代码之家  ›  专栏  ›  技术社区  ›  Declan McKenna

如何对静态属性进行单元测试?

  •  3
  • Declan McKenna  · 技术社区  · 7 年前

    我有一个 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)
        }
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Losiowaty    7 年前

    因为你的财产 static ,它们在代码的整个执行过程中都存在-它们不需要类的实际实例就可以保留在内存中。这就是为什么 静止的 静止的 in the docs, under "Type properties" .

    关于你手头的问题:
    tearDown() 方法,并将这些属性恢复到其空状态。 撕裂() XCTestCase

    override func tearDown() {
        ConferenceNumberDirectory.att = []()
        super.tearDown()
    }
    

    编辑:

    override func setUp() {
        super.setUp()
        ConferenceNumberDirectory.att = []()
    }
    

    此方法在运行每个测试用例之前运行。我强烈建议通读一遍 XCTest documentation.