mardi 6 septembre 2016

Unit testing private methods that return a promise using Jasmine/Karma

I am using Angular 1.5.7. Testing with Jasmine, PhantomJS, Karma and Grunt.

The code below is clearly not suppose to function, but it's the same methodology I used in the real code base so just go with it. :) Also, ignore the fact that I'm using $http.get(...) for what is returning the promise. It's just an example.

I have the following public method (vm is the controller alias, FYI):

vm.doSomething = function doSomething(someValue) {
    myPrivateMethod(someValue)
        .then(function(response) {
            vm.items = response;
        });

function myPrivateMethod(val) {
    return $http.get(...);  // promise returned (for instance)
}

How do I test the promise returned from the Private method?

I understand I can simply test against vm.items, but I clearly have to wait for the promise to be resolved. Can I use $q service in my test for this and how do I do so against a private method?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire