如何测试具有依赖关系的AngularJS过滤器?

如何测试具有依赖关系的AngularJS过滤器?,angularjs,testing,jasmine,Angularjs,Testing,Jasmine,我想测试一个AngularJS自定义过滤器,它有另一个过滤器依赖项。我怎样才能做到呢 我的过滤器如下所示: angular.module('myApp') .filter('addressLocations', function($filter) { return function(input, startFromIndex) { var arr = angular.copy(input); if (startFromIndex) { arr

我想测试一个AngularJS自定义过滤器,它有另一个过滤器依赖项。我怎样才能做到呢

我的过滤器如下所示:

angular.module('myApp')
  .filter('addressLocations', function($filter) {
    return function(input, startFromIndex) {
      var arr = angular.copy(input);
      if (startFromIndex) {
        arr = arr.splice(startFromIndex);
      }

      var locations = '';
      angular.forEach(arr, function(address) {
        locations += $filter('location')(address) + '<br>';
      });

      return locations;
    };
  });
angular.module('myApp')
.filter('addressLocations',函数($filter){
返回函数(输入,startFromIndex){
var arr=角度复制(输入);
如果(从索引开始){
arr=arr.拼接(起始自索引);
}
var位置=“”;
角度.forEach(arr,函数(地址){
位置+=$filter('location')(地址)+'
'; }); 返回地点; }; });
我的茉莉花测试现在看起来是这样的:

describe('addressLocations', function () {

  var filter;
  beforeEach(module('myApp'));

  beforeEach(inject(function(addressLocations) {
    // ??? how to inject $filter into `addressLocations` ???
    filter = addressLocations;
  }));

  it('should filter correctly', function() {
    var input = [{
      city: 'Karlsruhe',
      country: 'Germany'
    }, {
      city: 'Berlin',
      country: 'Germany'
    }, {
      city: 'Stuttgart',
      country: 'Germany'
    }, {
      city: '',
      country: 'Germany'
    }, {
      city: '',
      country: ''
    }];
    expect( filter(input, 1) ).toEqual('Berlin, Germany<br>Stuttgart, Germany<br>Germany<br>Unknown');

  });
});
描述('addressLocations',函数(){
var滤波器;
在每个模块之前(模块(‘myApp’);
beforeach(注入)函数(地址位置){
//?如何将$filter注入“addressLocations”???
过滤器=地址位置;
}));
它('应该正确过滤',函数(){
变量输入=[{
城市:“卡尔斯鲁厄”,
国家:“德国”
}, {
城市:“柏林”,
国家:“德国”
}, {
城市:“斯图加特”,
国家:“德国”
}, {
城市:'',
国家:“德国”
}, {
城市:'',
国家:''
}];
expect(filter(input,1)).toEqual('Berlin,Germany
Stuttgart,Germany
Unknown'); }); });
我知道上面的代码更像是一个集成测试,但是如果我知道如何在过滤器中注入依赖项,我会注入一个模拟函数来测试单元,而不是单元上的依赖项


提前谢谢

我不理解你的问题,但要测试过滤器,你应该注入$filter而不是addressLocations

var filter;

beforeEach(module('myApp'));

beforeEach(inject(function(_$filter_) {
    filter = _$filter_;
 }));


阅读更多:过滤器测试

您不需要手动将$Filter注入地址位置。我帮你打针。
expect( filter('addressLocations')(input, 1) )..