测试服务时无法在服务中注入$ q

时间:2016-10-18 11:58:23

标签: angularjs karma-jasmine

(function (angular) {
    'use strict';
    var testingModule= angular.module('testingModule', []); 
testingModule.factory('testService', [ '$q',
        function ($q) {
            var service = {
                    get: null
                },
                tokenDeferred = $q.defer(),

            service.getDataWithPromise = function () {

                        tokenDeferred.resolve({token: this.token, isAuthenticated: true});
                    } 
                });

                return tokenDeferred.promise;
            };
})(angular)

当我尝试为这种情况编写单元测试时,它总是给我$ $ undefined,Any idea什么可能出错?

Ius注入的方式如下:

   beforeEach(inject(function ($q_, _testService_) {
    q = $q
        fmOauthAccessTokenService = _testService_;

        scope = $rootScope.$new();

    }));

1 个答案:

答案 0 :(得分:1)

应该是这样的(请注意$q周围的双下划线):

beforeEach(inject(function (_$q_, _testService_) {
    q = _$q_;
        fmOauthAccessTokenService = _testService_;

        scope = $rootScope.$new();

    }));