gpt4 book ai didi

swift - 如何在 NEVPNManager 中 stub 连接属性(NEVPNConnection)?

转载 作者:可可西里 更新时间:2023-11-01 01:09:09 58 4
gpt4 key购买 nike

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

我首先为 NEVPNManager 创建了协议(protocol)

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)将其 stub 。

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

extension NEVPNConnection : ConnectionProtocol {}

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

Type 'NEVPNManager' does not conform to protocol '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 }

有什么建议吗?

最佳答案

模拟这个很棘手,因为 Apple 控制着 NEVPNManager 及其 NEVPNConnection 的实例化。

您看到的错误是因为您试图重新定义 connection 属性,而您不能这样做。 NEVPNManager 已经有 NEVPNConnection 类型的 connection 属性。

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

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

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

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

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

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

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

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

客户端类必须采用 NEVPNManagerProtocol 类型的对象,而不是 NEVPNManager 类型的对象,以便我们可以将模拟传递给它。

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"

关于swift - 如何在 NEVPNManager 中 stub 连接属性(NEVPNConnection)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49385623/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com