繁体   English   中英

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

[英]Injecting filter in directive for angular unit test

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

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);
            };
        }
    };
});

这是我的单元测试

“使用严格”;

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调用它时,我将不得不使用spyOn过滤器并返回一个模拟值,但不确定如何做到这一点。 谢谢

固定。 我必须包括具有inArray函数的主模块

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM