我想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 }
有什么建议吗?