2016-07-27 48 views
0

角单元测试指令错误“参数‘FN’不是一个函数,得到了对象”角单元测试指令错误“参数‘FN’不是一个函数,得到了对象”

这是我的代码:

angular.module("myApp.directives") 
    .directive("menu", ["$rootScope", function ($rootScope) { 
     return { 
      restrict: "E", 
      template: "<div class=\"tabs col-xs-12\" ng-transclude />", 
      transclude: true, 
      link: function (scope, elem, attrs) { 

       scope.test= function() { 
       };  

       $rootScope.$on("$viewContentLoaded", scope.test); 

      } 
     }; 
    }]); 

这是我的指令单元测试:

describe("menu component:", function() { 
    var element, 
     scope, 
     rootScope, 
     mockValue1 = "mock", 
     mockTabs = [ 
      "<menu name=\"" + mockValue1 + "\"><span>" + mockValue1 + "</span></menu>" 
     ]; 

    beforeEach(angular.mock.module("myApp.directives")); 

    beforeEach(inject(function($rootScope, $compile) { 
     rootScope = $rootScope; 
     scope = $rootScope.$new(); 

     element = $compile(mockTabs[0])(scope); 
     angular.element(document.body).append(element); 

     sinon.stub(rootScope, "$on"); 

     scope.$digest(); 
    })); 

    it("should have called viewContentLoaded on setup", function() { 
     expect(rootScope.$on.calledWith("$viewContentLoaded", scope.test)).toEqual(true); 
    }); 
}); 

回答

0

使用

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

,而不是

beforeEach(angular.mock.module("myApp.directives")); 
+0

这不能解决问题。 – AngularM

相关问题