mercredi 6 mai 2015

Best practices for unit testing backend calls with Angular/Mongo

I am writing an Angular application that has a Scala/Play backend and a MongoDB. I'm new to unit testing and I'm trying to wrap my head around how to test the Mongo calls.

The app was designed by someone else and I've inherited it. The backend is very generic there are only four API services defined - save, find, delete, and generate newID. The angular calls take in a collection name and maybe an ID.

I have written tests to make sure a particular method processes data in the way that I expect, but I am lost when it comes to testing the Mongo part.

For example, let's say I have two objects - Apples and Oranges. I could have methods such as these

myAppleSvc.getApple: (id) ->
    myDataSvc.find "apples", {id:id}
myOrangeSvc.getOrange: (id) ->
    myDataSvc.find "oranges", {id: id}

The lower level data svc would do something like this:

myDataSvc.find: (collection, query) ->
    deferred=$q.defer()
    @doPost "/api/v1/findJson/#{collection}", query, deferred
    return deferred.promise

The @doPost function is a helper method that handles the success/failure

doPost: (path, data, deferred) ->
    $http.post path, data
    .success (result) -> deferred.resolve result
    .error (result) -> deferred.reject result

There are some other things going on in the codebase, but this is the basic pattern.

I am trying to understand the best way to test this code. Most of my calls just return a promise to be resolved. How would I test the getOrange and getApple calls? As far as I can tell, all I can do is make sure they are returning a promise. With the dataSvc.find call, it seems all I need to do is make sure that it calls the correct URL and also returns a promise. Am I missing something?

Aucun commentaire :

Enregistrer un commentaire