mardi 1 décembre 2015

Jasmine unit test angular service return promise and access values from reponse

I'm calling a service in my unit test. The service seems to be available and the method is defined. I've created and spy and when I call the method I'm not receiving errors. When I try to access the response of the service call I get undefined. How can I test the returned values from the promise?

What am I missing?

describe("can I initialise the service method ", function () {
        var myMethodToCallDataService, $q;

        beforeEach(function(){
            module(moduleName);
            module('services');
            inject(function($injector, _$rootScope_,_$q_){
                $rootScope = _$rootScope_;
                $q=          _$q_;
                myMethodToCallDataService = $injector.get('myMethodToCallDataService');
            });

            var deferred = $q.defer();
            spyOn(myMethodToCallDataService, 'myMethodToCall').and.callFake(function() {
                var deferred = $q.defer();
                deferred.resolve('Remote call result');
                return deferred.promise;
            });
        });

        it("myMethodToCallDataService should be defined", function () {
            expect(myMethodToCallDataService).toBeDefined();
        });

        it("the myMethodToCall call should return a promise", function () {
            var stuff;
            myMethodToCallDataService.myMethodToCall()
                .then(function(data) {
                    stuff = data;

                });
            console.log(stuff);
        });

    });

Aucun commentaire:

Enregistrer un commentaire