如何在服务层中使用$ httpProvider的情况下测试.config? [英] How to karma test .config where $httpProvider is used in a service layer?
本文介绍了如何在服务层中使用$ httpProvider的情况下测试.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我的服务的样子
(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)
功能?我在离子中运行UI.请建议在beforeEach()区域中也定义.config
.
它与$ httpBackend有关系吗?
followed by the functions for popup and API services.
How to test the config($httpProvider)
function? I am running the UI in ionic. Please suggest the beforeEach() area to define the .config
also.
Has it got anything to do with $httpBackend?
推荐答案
我建议您测试请求是否具有正确的标头:
I suggest you to test if requests have correct 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
这篇关于如何在服务层中使用$ httpProvider的情况下测试.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文