如何在AngularJS中访问拦截器的'config'对象

时间:2015-09-18 13:47:09

标签: angularjs jasmine karma-runner

我正在测试角度拦截器。我想从茉莉花单元测试的上下文中查看配置上的内容。这是测试的代码......

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 个答案:

答案 0 :(得分: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);
});