gpt4 book ai didi

swift - 无法调用非函数类型的值,swift + cuckoo 模拟框架

转载 作者:行者123 更新时间:2023-12-05 04:38:25 25 4
gpt4 key购买 nike

我正在尝试用布谷鸟快速模拟服务。这是服务中的原始功能:

typealias GetAppConfigCompletionHandler = (_ response: AppConfig) -> Void

func getAppConfig(delegate: ErrorCoordinatorDelegate,
retryClosure: (() -> Void)?,
response responseCallback: @escaping GetAppConfigCompletionHandler) {
guard let appConfigUrl = "some/url"

HttpClientService<AppConfig>.makeRequest(errorCoordinatorDelegate: delegate,
retryClosure: retryClosure,
url: appConfigUrl) { appConfig in
responseCallback(appConfig)
}
}

然后在测试中我尝试模拟 EndPointService 以使其回调我的模拟值而不是调用 API:

guard let mockedAppConfig: AppConfig = JsonTestingHelper.decodeJSON(resourceName: "mockAppConfig",
model: AppConfig.self) else {
fail("failed to create mockAppConfig from JSON")
return
}

stub(endPointServiceMock) { mock in
when(mock.getAppConfig(delegate: any(),
retryClosure: any(),
response: any())).then { callback in
callback(mockedAppConfig) //error is here
}
}

这是生成的模拟函数的样子:

func getAppConfig(delegate: ErrorCoordinatorDelegate, retryClosure: (() -> Void)?, response responseCallback: @escaping GetAppConfigCompletionHandler)  {

return cuckoo_manager.call("getAppConfig(delegate: ErrorCoordinatorDelegate, retryClosure: (() -> Void)?, response: @escaping GetAppConfigCompletionHandler)",
parameters: (delegate, retryClosure, responseCallback),
escapingParameters: (delegate, retryClosure, responseCallback),
superclassCall:

Cuckoo.MockManager.crashOnProtocolSuperclassCall()
,
defaultCall: __defaultImplStub!.getAppConfig(delegate: delegate, retryClosure: retryClosure, response: responseCallback))

}

从它看起来它应该工作,但是我在回调(mockedAppConfig)行得到编译器提示:

Cannot call value of non-function type '(ErrorCoordinatorDelegate, (()-> Void)?, MockEndPointServiceType.GetAppConfigCompletionHandler)' (aka '(ErrorCoordinatorDelegate, Optional<(() -> ())>, (AppConfig) ->())')

我错过了什么?

最佳答案

错误消息虽然有点复杂,但可以准确告诉您问题所在;

当您调用 callback(mockedAppConfig) 时,您的 callback 变量实际上是一个具有 3 个参数的元组(一个 ErrorCoordinatorDelegate,一个可选的Void 函数,以及一个接收 AppConfig 参数的函数)。

为了修复这个错误,您需要做的就是:

callback.2(mockedAppConfig)

(这是您引用元组的任何未命名参数的方式)

或者,更好的是,你可以让你的 stub 看起来像这样:

stub(endPointServiceMock) { mock in
when(mock.getAppConfig(delegate: any(),
retryClosure: any(),
response: any())).then { _, _, callback in
callback(mockedAppConfig)
}
}

哪个更标准,更不晦涩:)

关于swift - 无法调用非函数类型的值,swift + cuckoo 模拟框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70604837/

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