角度单元测试指令错误“参数'fn'不是函数,得到了对象”

时间:2016-07-27 09:58:15

标签: angularjs unit-testing jasmine

Angular unit test directive错误“Argument'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);
    });   
});

1 个答案:

答案 0 :(得分:0)

使用

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

而不是

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