我正在尝试用布谷鸟快速模拟一项服务。这是服务中的原始功能:
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)行抱怨:
不能调用非函数类型的值'(ErrorCoordinatorDelegate, (() -> Void)?, MockEndPointServiceType.GetAppConfigCompletionHandler)' (aka '(ErrorCoordinatorDelegate, Optional<(() -> ())>, (AppConfig) -> ( ))')
我错过了什么?