javascript - Angular 服务 Jasmine 测试找不到 $http

标签 javascript angularjs service jasmine

按照这里的教程: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 模块.

关于javascript - Angular 服务 Jasmine 测试找不到 $http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25025905/

相关文章:

c# - 从 Windows 服务调用时,Process.Start 不起作用

python - 你能用 Python 创建 Mac OS X 服务吗?如何?

javascript - 我的 JS 项目中哪里可以存储大量 "business"数据/对象?

javascript - 访问 scriptlet 内的 Java Map

javascript - 如何在 javascript 中选择大整数中的第 n 个数字?

javascript - ng-table 与用户输入的过滤器一起使用

Android 在锁屏时执行功能

javascript - 如何在网站中定制新闻容器

javascript - Angular 中关于 IE 问题的占位符

AngularJS - 模板 : ngInclude directive