Angular.js promise not resolving when unit testing service with karma

前端 未结 4 1318
梦如初夏
梦如初夏 2021-01-18 19:03

I am trying to unit test an Angular.js service, and need to set an expect on a promise returned from a Mock service (using Jasmine). I am using the karma unit testing framew

4条回答
  •  無奈伤痛
    2021-01-18 19:28

    beforeEach(function(){
       var self=this;
        inject(function($rootScope,Facebook){
            self.$rootScope=$rootScope;
            self.Facebook=Facebook;
        });
    })
    
    it('resolves unless sourcecode broken',function(done){
        // I can't figure out how to do the equivalent of a $scope.$digest here. 
        var loginStatusPromise = this.FacebookService.getFacebookToken();
        loginStatusPromise.then(function(token) {
            expect(token).toBe('ValidToken');
            done();
        });
        $rootscope.$apply();
    
    });
    

    https://docs.angularjs.org/api/ng/service/$q

提交回复
热议问题