mercredi 31 mai 2017

How to test an angular service function nested promises?

Heres what my service kind of looks like

TestService.initializeDefaults = function() {
        var qPromise = $q.defer();
        $q.all({localResource : localResource.fetch(),
                item : itemResource.fetch()}).then(function(directories){

            //stuff happens with directories
            $q.all({
                thing1 : anotherThingFetch.fetch(),
                thing2: someThingFetch.fetch(),
                thing3: thingFetch.fetch()
            }).then(function(funData) {
                //stuff happens with the data

                preventiveServicesService.fetch().then(function() {

                    //stuff happens
                });

            });
        }.bind(this));
        return qPromise;
    };

And Im attempting to use karma to test that all the functions within this initializeDefaults method have run. Meaning that essentially all the fetch's happened. Heres what I have that works in the test so far:

it("should initialize defaults (Scenario 1)", function() {

            service.initializeDefaults();

            rootScope.$apply();

            expect(localResourceMock.fetch).toHaveBeenCalledWith();
            expect(itemResourceMock.fetch).toHaveBeenCalledWith();

Aucun commentaire:

Enregistrer un commentaire