繁体   English   中英

Swift:基类数组不调用子类函数实现

[英]Swift: Array of base class does not call subclass function implementation

我正在尝试根据NSObject子类SimplePing将虚假实例注入到类的单元测试中。 我的课有一个属性var simplePings: [SimplePing] ,在我的单元测试中,我将其设置为FakeSimplePing数组。 但是,当类进入数组并调用simplePing.start() ,它会调用SimplePing.start实现而不是FakeSimplePing的实现,即使在调试时我看到实例类型是FakeSimplePing。

当属性只是单个SimplePing时,单元测试将使用FakeSimplePing.start并通过测试。 这与Swift和超类数组有关吗?

class Pinger : NSObject {
   private var simplePings: [SimplePing] = []

   func pingLocation(location: Location) -> Signal<Double, NoError> {
       let simplePings = location.serverIPs.map { (serverIP: String) -> SimplePing in
           let simplePing = SimplePing(hostName: serverIP)
           simplePing?.delegate = self
           return simplePing
    }
       configureDependencies(simplePings)
       simplePings.forEach { $0.start() }

       return signal
   }

   func configureDependencies(simplePings: [SimplePing]) {
       if self.simplePings.isEmpty {
           self.simplePings = simplePings
       }
   }
}



class FakeSimplePing: SimplePing {
    var receivedStart = false
    var receivedSendPingWithData = false
    var fakeHostName: String!
    override var hostName: String {
        return fakeHostName
    }

    convenience init(hostName: String) {
        self.init()
        fakeHostName = hostName
    }

    override func start() {
        // This does not get called
        receivedStart = true
        delegate?.simplePing?(self, didStartWithAddress: nil)
        delegate?.simplePing?(self, didReceivePingResponsePacket: nil)
    }

    override func sendPingWithData(data: NSData!) {
        receivedSendPingWithData = true
    }
}

和失败的测试:

            beforeEach {
                fakeSimplePing = FakeSimplePing(hostName: serverIP)
                fakeSimplePing.delegate = pinger
                pinger.configureDependencies([fakeSimplePing])
            }

            it("pings server with data") {
                pinger.pingLocation(location)

                expect(fakeSimplePing.receivedSendPingWithData).toEventually(beTrue())
            }

问题(我相信...)是在pingLocation中命名

在行中

let simplePings = location.serverIPs.map { .... 

您使用与财产相同的名称

private var simplePings: [SimplePing] = []

因此您可能会认为您正在使用let定义一个新变量,但实际上,您可能只是使用您的属性并在途中对其进行了更改,因此它从地图返回时更改为SimplePing数组。

尝试将您的方法更改为:

func pingLocation(location: Location) -> Signal<Double, NoError> {
       let tempSimplePings = location.serverIPs.map { (serverIP: String) -> SimplePing in
           let simplePing = SimplePing(hostName: serverIP)
           simplePing?.delegate = self
           return simplePing
    }
       configureDependencies(tempSimplePings)
       simplePings.forEach { $0.start() }

       return signal
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM