在角度单位测试的指令中注入过滤器

时间:2015-02-18 19:48:54

标签: javascript angularjs unit-testing

这可能很简单但我无法在任何地方找到任何解决方案。现在我有一个使用过滤功能的指令。一切正常,但我正在尝试为它创建一个单元测试,它正在给我

Error: [$injector:unpr] Unknown provider: inArrayFilterProvider <- inArrayFilter

这是我的指示

var directiveApp = angular.module('myApp.directives',[]);
directiveApp.directive('navbar', function($filter) {

    return {
        templateUrl: 'partials/navbar.html',

        controller: function($scope, $location) {

                        $scope.checkLocation = function(){
                            return $location.path();
                        };
            $scope.isActive = function (viewLocation) {

                var locations = viewLocation.split(',');
                //return viewLocation === $location.path();
                var path = '/' + $location.path().split('/')[1];
                //return $filter('inArray')(locations, $location.path());
                return $filter('inArray')(locations, path);
            };
        }
    };
});

这是我的单元测试

'use strict';

describe('myApp.directives module', function() {

  beforeEach(module('myApp.directives'));

  describe('navbar directive', function() {
      var $scope,controller,template;
      beforeEach(inject(function($rootScope,$compile,$location,$filter,$templateCache){
          $scope = $rootScope.$new();

          $templateCache.put('partials/navbar.html','I am here');
          spyOn($location,'path').andReturn("/festivals");
          var element = angular.element('<div  data-navbar=""></div>');
          template = $compile(element)($scope);
          controller = element.controller;
          $scope.$digest();
      }));
    it('Check Festival nav', function() 
    {
        expect($scope.isActive('/festivals')).toBeTruthy();
    });
  });
});

我认为在使用inArray调用并且返回模拟值但不确定如何执行时,我将不得不对其进行过滤。 谢谢

1 个答案:

答案 0 :(得分:0)

固定。 我必须包含具有inArray功能的主模块

describe('myApp.directives module', function() {
var $filter;
  beforeEach(module('myApp'),function(_$filter_){
      $filter = _$filter_;
  });
.../*Rest as Before*/
...
...});