所以我是JavaScript和AngularJS的新手,因此我的代码还不如它应该的那么好,但它正在改进.然而,我开始学习并实现一个带有REST后端的简单登录页面.提交登录表单后,将返回一个身份验证令牌,并将其设置为默认的http-header属性,如下所示
$http.defaults.headers.common['X-AUTH-TOKEN'] = data.authToken;
Run Code Online (Sandbox Code Playgroud)
每当我手动测试时,这都可以正常工作,但这不是我要去的方法,所以我想实现一个单元测试,它检查是否设置了X-AUTH-TOKEN标头.
有没有办法用$ httpBackend检查?例如,我有以下测试:
describe('LoginController', function () {
var scope, ctrl, $httpBackend;
// Load our app module definition before each test.
beforeEach(module('myApp'));
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
// This allows us to inject a service but then attach it to a variable
// with the same name as the service.
beforeEach(inject(function (_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;
scope = $rootScope.$new();
ctrl = $controller('LoginController', {$scope: scope}, {$http: $httpBackend}, {$location: null});
})); …Run Code Online (Sandbox Code Playgroud) angularjs ×1