简体   繁体   中英

Injecting filter in directive for angular unit test

this might be simple but I cant find any solution anywhere. Right now i have a directive which uses a filter function. Everything works fine but I am trying to create a unit test for it and it is giving me

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

This is my directive

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

And this is my unit test

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

I think i will have to spyOn filter when it is called with inArray and return a mock value but not really sure how to do it. Thank you

Fixed. I had to include my main module which had inArray function

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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