Angular服务jasmine test找不到$http
angular service jasmine test can't find $http
按照这里的教程: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
模块
相关文章:
- 气质的“nestRemoting()”有时可以'找不到关系
- 错误405:向Java控制器(Ajax)发送JSON时找不到POST方法
- 找不到模块捆绑包
- for循环中的javascript if语句找不到==
- 为什么JavaScript可以'找不到给定的InnerHTML并返回Cannot set property
- 失败:等待Protractor与页面同步时出错:“”;在窗口上找不到角度”;
- Babel编译错误:找不到模块核心js/library/fn/get迭代器
- [Vue warn]:找不到元素
- ngRoute找不到模板
- 未能加载,因为找不到支持的源.当播放HTML5音频元素时
- 因果报应-找不到模块:错误:无法解析模块'scs'
- Angular 2:在本地.json文件上找不到文件
- 角度:原点http://localhost在Access Control Allow Origin标头中找不到
- 火狐插件错误:在 resource://gre/modules/commonjs/http.js 找不到模块“路径”
- JQuery - 找不到 HTTP 404 - 服务器未找到与请求的 URI 匹配的任何内容
- 节点 js 找不到响应体 HTTP 创建服务器
- METEOR:如果找不到404资产,如何实施回退?HTTP获取
- Angular服务jasmine test找不到$http
- 在req中找不到我的json文件.通过HTTP post请求发送时
- 找不到 Angular HTTP 服务 URL 的问题