Reputation: 4237
Following the tutorial here: http://www.benlesh.com/2013/06/angular-js-unit-testing-services.html the following jasmine test should work -- but doesn't. I've reduced the test down to the simplest possible thing to test. Unlike the example test I linked, I can't get this to pass. Any idea what I'm doing wrong?
The service under test:
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
};
}]);
The test:
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);
});
});
The error message:
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/
Upvotes: 1
Views: 786
Reputation: 4318
var appServices = angular.module('MyApp.services', ['$http', '$rootScope']);
should be
var appServices = angular.module('MyApp.services', []);
$http
and $rootScope
are not modules so the error was appServices
could not find $http
module.
Upvotes: 5