簡體   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