mercredi 3 février 2016

Unit Testing Angular service with nested promises with $http calls

I am, using jasmine, trying to test the following function as a part of an angular service

service.initialize = function (params) {
    var defer = $q.defer();
    $q.all([service.getSpecs(params), service.getValData(params), service.getRelatedMeasures(params)])
        .then(function (values) {
            $q.all([service.getIssues(), service.getData(params, values)])
                .then(function (values) {
                    defer.resolve(values);
                });
        });
    return defer;
};

Each of the referenced functions in both of the $q.all() statements contains an $http call.

In my test, I am mocking all of the $http calls using $httpBackend. My test looks something like this:

describe('the intialize function', function () {
    var mockParams = {};
    beforeEach(function () {
        svc.initialize(mockParams);
        $httpBackend.flush();
    });
    it('should call the getSpecs function', function () {
        expect(editorSvc.getSpecs).toHaveBeenCalledWith(mockParams);
    });
});

However, I get an error that there are still $http requests in the queue. I have tried calling $httpBackend.flush() again, and also including $rootScope.$apply() in various orders and I can't get it to resolve the inner promise.

Any way around this?

Aucun commentaire:

Enregistrer un commentaire