如何在服务层中使用 $httpProvider 的情况下对 .config 进行业力测试?
How to karma test .config where $httpProvider is used in a service layer?
我的服务是这样的
(function(){
'use strict';
angular.module('gls.service', [])
.config(config)
.service('popup', popup)
.service('API', api);
/* Config */
config.$inject = ['$httpProvider'];
function config($httpProvider) {
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.headers.get = {
'Content-Type': 'application/json'
};
$httpProvider.defaults.headers.post = {
'Content-Type': 'application/json'
};
$httpProvider.defaults.headers.put = {
'Content-Type': 'application/json'
};
}
后面是弹出窗口和API服务的功能。
如何测试config($httpProvider)
函数?我是 ionic 中的 运行 UI。请建议 beforeEach() 区域也定义 .config
。
它与 $httpBackend 有什么关系吗?
我建议你测试请求是否正确 header:
// Assume $httpBackend and $http have been properly injected above
it('should have correct Content-Type header on GET request', function() {
$httpBackend.expectGET('/api-call', function(headers) {
return headers['Content-Type'] === 'application/json';
}).respond(200, {});
$http.get('/api-call');
$httpBackend.flush();
});
// ... and do the same for POST and PUT requests
我的服务是这样的
(function(){
'use strict';
angular.module('gls.service', [])
.config(config)
.service('popup', popup)
.service('API', api);
/* Config */
config.$inject = ['$httpProvider'];
function config($httpProvider) {
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.headers.get = {
'Content-Type': 'application/json'
};
$httpProvider.defaults.headers.post = {
'Content-Type': 'application/json'
};
$httpProvider.defaults.headers.put = {
'Content-Type': 'application/json'
};
}
后面是弹出窗口和API服务的功能。
如何测试config($httpProvider)
函数?我是 ionic 中的 运行 UI。请建议 beforeEach() 区域也定义 .config
。
它与 $httpBackend 有什么关系吗?
我建议你测试请求是否正确 header:
// Assume $httpBackend and $http have been properly injected above
it('should have correct Content-Type header on GET request', function() {
$httpBackend.expectGET('/api-call', function(headers) {
return headers['Content-Type'] === 'application/json';
}).respond(200, {});
$http.get('/api-call');
$httpBackend.flush();
});
// ... and do the same for POST and PUT requests