Question

I have an ExampleModel that calls to an ExampleService that retrieves data from our backend. I can't figure out how to write unit tests for my application; which is structured as shown below:

ExampleService

public function retrieveMyToDoList(parameters):Promise
{
    var promise:Promise = performRequest({request: "call to backend", parameters:    values, session_id: clientModel.sessionID});
    promise.addResultProcessor(parseRetrieveToDoListResult);
    return promise;
}

protected function parseRetrieveToDoListResult(data:Object, callback:Function):void
{
    does some JSON parsing into an object
    callback(null, object containing my retrieved data)
}

ExampleModel

public function getMyToDoList():Promise
{
    var promise:Promise = exampleService.retrieveToDoList(parameters);
    promise.addResultHandler(onGetToDoListResult);
    promise.addErrorHandler(onGetToDoListError);
    return promise;
}

private function onGetHeadrsByUserResult(promise:Promise):void
{
    // where this event will be listened to by mediators etc
    dispatchEvent(new ResponseEvent(GOOD_RESULT));
}

private function onGetHeadrsByUserError(promise:Promise):void
{
    dispatchEvent(new ResponseEvent(BAD_RESULT));
}

I'm trying to use asmock to mock my Service so that I can test my Model and how it handles the various results in the resulting Object but how do I mock the callback? I saw examples where the return values were mocked but in my case I'm using the Promise and callback and I'm not too sure how to go ahead.

If someone could please advise. Thanks!

Was it helpful?

Solution

You can let the mock service return a real promise and call the handleResult method of the promise directly.

FYI: it's not a good idea to have a direct dependency from the model to the service. You should let the service manipulate the model, or pass the results from the service to a command which will manipulate the model. Models should never depend on anything else than helper classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top