Javascript 如何用jasmine在英语中测试ng形式项目的效度

Javascript 如何用jasmine在英语中测试ng形式项目的效度,javascript,angularjs,angularjs-directive,jasmine,Javascript,Angularjs,Angularjs Directive,Jasmine,我尝试用jasmine对angularjs指令进行单元测试。 我的指令包括在模糊事件中完成一个带前导零的id,然后检查该id是否已经存在于id列表中(由json文件提供)。 如何测试表单的有效性(id.$error.unique) 提前感谢您的帮助 该指令: angular.module('bdd.directives').directive('bddUnique', function () { function formatIdentifier(id) { if (id

我尝试用jasmine对angularjs指令进行单元测试。 我的指令包括在模糊事件中完成一个带前导零的id,然后检查该id是否已经存在于id列表中(由json文件提供)。 如何测试表单的有效性(id.$error.unique) 提前感谢您的帮助

该指令:

angular.module('bdd.directives').directive('bddUnique', function () {
    function formatIdentifier(id) {
        if (id) {
            var zeroes = '0000000000';
            var formattedId = zeroes.substring(0, zeroes.length - id.length) + id;
            return formattedId;
        }
        return '';
    }

    return {
        restrict : 'A',
        require : 'ngModel',

        link : function (scope, element, attrs, ctrl) {
            element.bind('blur', function (e) {
                ctrl.$setValidity('unique', true);

                if (ctrl && element.val()) {

                    var identifiers = scope.$eval(attrs.bddUnique);
                    var currentValue = formatIdentifier(element.val());

                    ctrl.$setViewValue(currentValue);
                    ctrl.$render();
                    var idFound = _.find(identifiers, {
                        id : currentValue
                    });

                    if (idFound !== undefined) {
                        ctrl.$setValidity('unique', false);
                    }
                }
                scope.$apply();
            });

        }
    }
});
单元测试

describe('uniqueDirective', function () {
    var mockCompile, mockScope;

    var changeInputValue;

    var htmlFragment = '<div ng-form name="myForm"><input type="text" ng-model="id" bdd-unique="identifiers"/>';
    htmlFragment += '<p id="errorMsg" ng-show="myForm.numero.$invalid">identifier already exist</p></div>';

    // / / / / load the myModule.directives module, which contains the directive
    beforeEach(module('myModule', function ($provide) {
        $provide.value('resolver', {
            identifiers : function () {
                return readJSON('app/assets/mocks/identifiers.json');
            }
        });
    }));

    beforeEach(inject(function (_$compile_, _$rootScope_, resolver) {
        mockCompile = _$compile_;
        mockScope = _$rootScope_.$new();
        mockScope.identifiers = resolver.identifiers();

         elem = angular.element(htmlFragment);
         template = mockCompile(elem)(mockScope);
         mockScope.$digest(); 

    }));

    it('id already exists', function () {


        var input = template.find('input'); 
        input.val('15);
        input.triggerHandler('blur');


        expect(mockScope.id).toBe('0000000015'); // OK
        // expect(myForm.numero.$error.unique.$valid).toBeFalsy();  //how to specify ???

    });

});
description('uniqueDirective',函数(){
var mockCompile,mockScope;
var-changeInputValue;
var htmlFragment='';
htmlFragment+='

标识符已存在; /////加载myModule.directives模块,其中包含该指令 每个之前(模块('myModule',函数($provide){ $provide.value('resolver'{ 标识符:函数(){ 返回readJSON('app/assets/mocks/identifiers.json'); } }); })); beforeach(注入函数($compile,$rootScope,解析器){ mockCompile=$compile; mockScope=\$rootScope\$new(); mockScope.identifiers=resolver.identifiers(); elem=角度元素(htmlFragment); 模板=mockCompile(elem)(mockScope); mockScope.$digest(); })); 它('id已存在',函数(){ var input=template.find('input'); input.val('15); input.triggerHandler('blur'); expect(mockScope.id).toBe('0000000015');//确定 //expect(myForm.numero.$error.unique.$valid).toBeFalsy();//如何指定??? }); });


已解决

将name=“numero”添加到htmlFragment,使其成为

 var htmlFragment = '<div ng-form name="myForm"><input type="text" 
    name='numero' ng-model="id" bdd-unique="identifiers"/>';
测试正常情况

var form = mockScope.myForm;
expect(form.numero.$error.unique).toBeUndefined();
var form = mockScope.myForm;
expect(form.numero.$error.unique).toBeUndefined();