Angular服务jasmine test找不到$http

angular service jasmine test can't find $http

本文关键字:http 找不到 test 服务 jasmine Angular      更新时间:2023-09-26

按照这里的教程:http://www.benlesh.com/2013/06/angular-js-unit-testing-services.html下面的jasmine测试应该可以工作——但是没有。我把测试简化到最简单的东西。不像我链接的例子测试,我不能让它通过。知道我哪里做错了吗?

待测服务:

var appServices = angular.module('MyApp.services', ['$http', '$rootScope']);
  appServices.factory('Auth', ['$http', '$rootScope', function ($http, $rootScope) {
    var accessLevels = routingConfig.accessLevels,
        userRoles = routingConfig.userRoles,
        currentUser = { username: '', role: userRoles.public };
    function changeUser(user) {
        angular.extend(currentUser, user);
    }
    return {
        authorize: function (accessLevel, role) {
            if (role === undefined) {
                role = currentUser.role;
            }
            return accessLevel.bitMask & role.bitMask;
        },
        isLoggedIn: function (user) {
            if (user === undefined) {
                user = currentUser;
            }
            return user.role.title === userRoles.user.title;
        },
        login: function (user, success, error) { //ignore jslint
            user.role = userRoles.user;
            changeUser(user);
            $rootScope.$broadcast('login');
            success(user);
        },
        logout: function (success, error) { //ignore jslint
            changeUser({
                username: '',
                password: '',
                role: userRoles.public
            });
            success();
        },
        accessLevels: accessLevels,
        userRoles: userRoles,
        user: currentUser
    };
}]);

测试:

describe('services tests', function () {
    beforeEach(function () {
        module('MyApp.services');
        // get your service, also get $httpBackend
        // $httpBackend will be a mock, thanks to angular-mocks.js
        inject(function (_Auth_, $httpBackend) {
            Auth = _Auth_;
            httpBackend = $httpBackend;
        });
    });
    it('should have an authorize function', function () {
        expect(angular.isFunction(Auth.authorize)).toBe(true);
    });
});

错误信息:

Error: [$injector:modulerr] Failed to instantiate module MyApp.services due to:
Error: [$injector:modulerr] Failed to instantiate module $http due to:
Error: [$injector:nomod] Module '$http' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.2.13/$injector/nomod?p0=%24http
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:78:12
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:1531:17
    at ensure (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:1456:38)
    at module (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:1529:14)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:3632:22
    at Array.forEach (native)
    at forEach (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:304:11)
    at loadModules (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:3626:5)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:3633:40
    at Array.forEach (native)
http://errors.angularjs.org/1.2.13/    

var appServices = angular.module('MyApp.services', ['$http', '$rootScope']);

应该

var appServices = angular.module('MyApp.services', []);

$http$rootScope不是模块,所以错误是appServices找不到$http模块