Karma:如何使用依赖项对过滤器进行单元测试?

Karma: how to unit test the filter with dependencies?

我正在尝试对依赖于其他服务的过滤器进行单元测试。我已经阅读了很多关于这个问题的答案,但没有解决我的问题。 我用 karma/Jasmine

配置如下

  // Karma configuration
  // Generated on Thu Nov 26 2015 07:47:24 GMT+0100 (Västeuropa, normaltid)

 module.exports = function(config) {
 config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'
],


// list of files / patterns to load in the browser
files: [
 'lib/angular.min.js',
 'lib/angular-mocks.js',

 'test.js',
 'app/js/services/*.js',

 'app/js/filters/*.js'

  ],


// list of files to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-          preprocessor
 preprocessors: {
  },

我的申请如下

// filter

var app=angular.module('app.filter',[]);

app.filter("filterName",['serviceName',function(ServiceName){
return function(input1,input2)
{
//some code
}]);


// service

 var app=angular.module('app.service',[]);
 app.service("serviceName",      [$log,$windows,$route',function($log,$windows,$route){
//some functions

}]);

测试

   describe('test filter with services dependencies',function()
{

     beforeEach(module('app.service')); 
      beforeEach(module('app.filter'));

      var FilterNAMe, ServiceNAMe ;


 beforeEach(inject(function(_ServiceNAMe _,_FilterNAMe_) // i get error here
 {
  FilterNAMe = _FilterNAMe_;
  ServiceNAMe= _ServiceNAMe_;

 }));

  it('',function()
   {
    //some test;

  });



  });

我收到以下错误

error:[$injector:unpr] http://errors.angularjs.org/1.4.0-rc.2/$injector/unpr?=%24serviceNameProvider

Error: Declaration location at windows.inject.angular.mock.inject

首先不需要依赖服务所在的模块(app.service)

否则在调用当前模块时使用provide函数提供服务

var $filter,  FilterNAMe ;
beforeEach(function () {

angular.mock.module("app.filter", function ($provide) {
            $provide.value('serviceName');
        });
});

然后注入过滤器

    beforeEach( inject(function ( $filter) { //<-- Get the filter provider

    FilterNAMe =$filter('FilterNAMe ',{});

    }));

查看答案 Injecting dependent services when unit testing AngularJS services