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);
});
});
這不能解決問題。 – AngularM