角度服务茉莉花测试无法找到$ http

时间:2014-07-29 22:16:48

标签: javascript angularjs service jasmine

按照这里的教程:http://www.benlesh.com/2013/06/angular-js-unit-testing-services.html以下茉莉花测试应该有效 - 但不是。我已经将测试简化为最简单的测试方法。与我链接的示例测试不同,我无法通过。知道我做错了吗?

受测试服务:

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/    

1 个答案:

答案 0 :(得分:5)

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

应该是

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

$http$rootScope不是模块,因此错误appServices无法找到$http模块。