2015-09-18 4 views
1

Я тестирую угловой перехватчик. Я хочу проверить что-то в конфиге из контекста теста жасминового блока. Вот код для теста ....Как получить доступ к «config» объекту перехватчика в AngularJS

it('should set something on the config', function(){ 

    $http.get('/myEndpoint'); 
    $httpBackend.flush(); 

    expect('????? -- I want to check the config.property here'); 

}); 

Вот код производства ...

angular.module('app.core').factory('MyInterceptor', function MyInterceptor($injector) { 

    return { 
     request: function(config) { 

     config.property = 'check me in a test'; 
     return config; 
     }, 

);

Мой вопрос в том, как я могу проверить config.property из теста?

ответ

1

должно работать:

var config; 
$httpBackend.expectGET('/myEndpoint').respond(200); 
$http.get('/myEndpoint').then(function(response) { 
    config = response.config; 
}); 
$httpBackend.flush(); 
expect(config.property).toBe('check me in a test'); 

Но это почти интеграционный тест. Почему бы не создать реальный единичный тест:

it('should set something on the config', function() { 
    var input = {}; 
    var config = MyInterceptor.request(input); 
    expect(config.property).toBe('check me in a test'); 
    expect(config).toBe(input); 
}); 
Смежные вопросы