Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
Cannot call value of non-function type, swift + cuckoo mocking framework

I'm trying to mock a service with cuckoo in swift. Here's the original function in the service:

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

Then in the tests I'm trying to mock the EndPointService to make it call back my mocked value instead of calling an 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
    }
}

And this is how the generated mock function looks like:

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

From what it looks like it should work, however I get compiler complaining at the callback(mockedAppConfig) line:

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

What am I missing?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The error message, while a bit complex, tells you exactly what the issue is;

When you're calling callback(mockedAppConfig), your callback variable is actually a tuple with 3 parameters (a ErrorCoordinatorDelegate, an optional Void function, and a function that takes in a AppConfig parameter).

In order to fix this error, all you need to do is:

callback.2(mockedAppConfig)

(This is how you reference any unnamed parameter of a tuple)

Or, better yet, you could make your stub look like this:

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

Which is a bit more standard and less obscure :)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda