小编Osc*_*car的帖子

单元测试AngularJS指令

我如何对我的指令进行单元测试?

我拥有的是什么

angular.module('MyModule').
    directive('range', function() {
        return {
            restrict: 'E',
            replace: true,
            scope: {
                bindLow: '=',
                bindHigh: '=',
                min: '@',
                max: '@'
            },
        template: '<div><select ng-options="n for n in [min, max] | range" ng-model="bindLow"></select><select ng-options="n for n in [min, max] | range" ng-model="bindHigh"></select></div>'
    };
})
Run Code Online (Sandbox Code Playgroud)

在我的单元测试中,我想从一个非常简单的测试开始

describe('Range control', function () {
    var elm, scope;

    beforeEach(inject(function(_$compile_, _$rootScope) {
        elm = angular.element('<range min="1" max="20" bind-low="low" bind-high="high"></range>');

        var scope = _$rootScope_;
        scope.low = 1;
        scope.high = 20;
        _$compile_(elm)(scope);
        scope.$digest();
    }));

    it('should render two …
Run Code Online (Sandbox Code Playgroud)

unit-testing angularjs angularjs-directive

6
推荐指数
1
解决办法
5053
查看次数