有没有办法测试具有许多参数的Angular指令

时间:2015-12-24 04:15:49

标签: angularjs jasmine karma-runner karma-jasmine

我一直在寻找正确的方法来为具有许多参数的angular指令编写unittest。

(function () {
    'use strict';

    angular
        .module('svt')
        .directive('elementInteractor', draggable);

    draggable.$inject = [
        '$document', 'settingConst', 'rpElementEditorSvc', 'reportElementDataSvc', 'reportPageSvc', 'reportEditorSvc',
        'angularScopeUtilSvc'
    ];

    function draggable($document, settingConst, rpElementEditorSvc, reportElementDataSvc, reportPageSvc, reportEditorSvc,
        angularScopeUtilSvc) {
        return {
            restrict: 'A',
            scope: {
                pageData: '=',
                questionData: '='
            },
            link: function (scope, element) {
                var maxzIndexToReset = settingConst.report.maxzIndexToReset;
                var minElementSize = settingConst.report.minElementSize;
                var draggablePageConfig = {
                    padding: 0,
                    borderWidth: 2,
                    borderColor: '#244f95'
                };
                var resizeConstants = {
                    main: 'resize',
                    left: 'resize-left',
                    right: 'resize-right',
                    top: 'resize-top',
                    bottom: 'resize-bottom',
                    topLeft: 'resize-top-left',
                    topRight: 'resize-top-right',
                    bottomLeft: 'resize-bottom-left',
                    bottomRight: 'resize-bottom-right'
                };
                var startX = 0, startY = 0, x = 0, y = 0, z = 0;
                var isResizeMode = false,  resizeClass = '', width = 0, height = 0;
                var orginalX = 0, orginalY = 0, orginalZ = 0;
                var elementPosition = {};

                init();
              .......
            }
        };
    }
})();

我试图通过这种方式写作,但它没有工作

describe("Testing elementInteractor directive", function () {
    var element, scope, $compile,
        $document,settingConst,
        rpElementEditorSvc, reportElementDataSvc, reportPageSvc, reportEditorSvc, angularScopeUtilSvc;
    beforeEach(angular.mock.module('ngMockE2E'));
    beforeEach(module('svt', '../page/reportPage.html'));


    beforeEach(inject(function (_$compile_, $rootScope, $injector) {
        scope = $rootScope;
        scope.questionData = {};
        scope.pageData = {};
        $document = $injector.get('$document');
        settingConst = $injector.get('settingConst');
        rpElementEditorSvc = jasmine.createSpyObj('rpElementEditorSvc', ['getzIndexMax']);
        reportElementDataSvc = jasmine.createSpyObj('reportElementDataSvc', ['updateElementSize','updateElementPositions']);
        reportPageSvc = jasmine.createSpyObj('reportPageSvc', ['getWorkingElementIds', 'getEditingElement', 'setDeselectedElementId', 'setSelectedElementId']);
        reportEditorSvc = jasmine.createSpyObj('reportEditorSvc', ['setShowedMarginPageId']);
        angularScopeUtilSvc = jasmine.createSpyObj('angularScopeUtilSvc', ['safeApply']);
        $compile = _$compile_;
        element = angular.element(
            '<div element-interactome page-data="page-data" question-data="question-data"></div>');
        $compile(element, {
            $document: document,
            settingConst: settingConst,
            rpElementEditorSvc: rpElementEditorSvc,
            reportElementDataSvc: reportElementDataSvc,
            reportPageSvc: reportPageSvc,
            reportEditorSvc: reportEditorSvc,
            angularScopeUtilSvc: angularScopeUtilSvc
        })(scope);
        scope.$digest();
    }));

    it("should display the values", function () {
          .....
    });
});

这里的任何人都可以帮助我找到在业力和茉莉环境中有效编写单元测试的方法。

0 个答案:

没有答案