2

我想NetworkExtension通过协议扩展现有的类,以便对我的代码进行单元测试。

我首先创建了协议NEVPNManager

protocol NEVPNManagerProtocol {
    var connection : ConnectionProtocol { get } // <-- Doesn't work
    func loadFromPreferences(completionHandler: @escaping (Error?) -> Swift.Void)
    func saveToPreferences(completionHandler: ((Error?) -> Swift.Void)?)
}

extension NEVPNManager: NEVPNManagerProtocol {}

然后是单独的connection属性协议将其存根。

protocol ConnectionProtocol {
    var status: NEVPNStatus { get }
    func stopVPNTunnel()
    func startVPNTunnel() throws
}

extension NEVPNConnection : ConnectionProtocol {}

在 NEVPNManager 中,我可以看到我正在确认属性签名,但 Xcode 不相信我并声称:

类型“NEVPNManager”不符合协议“NEVPNManagerProtocol”

它尝试像这样自动更正它:

extension NEVPNManager: NEVPNManagerProtocol {
    var connection: ConnectionProtocol {
        <#code#>
    }
}

但是检查签名NEVPNManager,对我来说似乎是正确的:

     /*!
     * @property connection
     * @discussion The NEVPNConnection object used for controlling the VPN tunnel.
     */
    @available(iOS 8.0, *)
    open var connection: NEVPNConnection { get }

有什么建议吗?

4

1 回答 1

4

嘲笑这件事很棘手,因为 Apple 控制着NEVPNManager及其NEVPNConnection.

您看到的错误是因为您正在尝试重新定义该connection属性,而您不能这样做。NEVPNManager已经有一个connectiontype 的属性NEVPNConnection

我们可以connection使用您的第一个协议(修改)和几个模拟类的组合来模拟该属性。

首先,需要稍微调整协议:

protocol NEVPNManagerProtocol {
    var connection : NEVPNConnection { get } // <-- has to be this type
    func loadFromPreferences(completionHandler: @escaping (Error?) -> Swift.Void)
    func saveToPreferences(completionHandler: ((Error?) -> Swift.Void)?)
}

extension NEVPNManager: NEVPNManagerProtocol {}

接下来,我们需要一个模拟连接,因为该connection属性必须是 type 的类NEVPNConnection,或者继承自该类型的类。在这里引入协议似乎没有太多好处,因为我们试图模拟类的行为,我们可以用模拟更直接地做到这一点。

class MockNEVPNConnection: NEVPNConnection {
    override var status: NEVPNStatus {
        return NEVPNStatus.connected //or whatever
    }
    override func stopVPNTunnel() {
        print("MockNEVPNConnection.stopVPNTunnel")
    }
    override func startVPNTunnel() throws {
        print("MockNEVPNConnection.startVPNTunnel")
    }
 }

最后,我们需要一个返回模拟连接的模拟管理器类。使用模拟管理器是我能够注入模拟连接的唯一方法。

模拟管理器符合NEVPNManagerProtocol并返回我们的模拟连接对象。(注意:当尝试直接从 继承时NEVPNManager,我的游乐场在实例化模拟时崩溃了。)

class MockNEVPNManager: NEVPNManagerProtocol {
    var connection: NEVPNConnection {
        return MockNEVPNConnection()
    }
    func loadFromPreferences(completionHandler: @escaping (Error?) -> Swift.Void) {
        print("MockNEVPNManager.loadFromPreferences")
    }
    func saveToPreferences(completionHandler: ((Error?) -> Swift.Void)?) {
        print("MockNEVPNManager.saveToPreferences")
    }
}

客户端类必须采用 typeNEVPNManagerProtocol和 not的对象NEVPNManager,以便我们可以将 mock 传递给它。

class MyClient {
    let manager: NEVPNManagerProtocol
    init(manager: NEVPNManagerProtocol) {
        self.manager = manager
    }
}

在现实生活中,我们可以将真正的经理传递给我们的客户:

let myClient = MyClient(manager: NEVPNManager.shared())

在我们的测试中,我们可以通过模拟:

let myMockedClient = MyClient(manager: MockNEVPNManager())

并在连接上调用方法:

try? myMockedClient.manager.connection.startVPNTunnel()
//prints "MockNEVPNConnection.startVPNTunnel"
于 2018-03-21T02:22:56.470 回答