2016-06-19 35 views
2

这是我的服务看起来像

(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)函数?我正在用离子形式运行用户界面。请建议beforeEach()区域来定义.config。 它与$ httpBackend有什么关系?

回答

0

我建议您测试要求有正确的头:

// 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