2016-10-13 4 views
0

入力を検証する基本ページを作成しました。ジャスミンで角度検査の単位検証を行う方法

単位テストフォームの検証方法

JS

// create angular app 
var validationApp = angular.module('validationApp', []); 

// create angular controller 
validationApp.controller('mainController', function($scope) { 

var vm = this; 
// function to submit the form after all validation has occurred    
vm.submitForm = function() { 

    // check to make sure the form is completely valid 
    if ($scope.userForm.$valid) { 
    alert('our form is amazing'); 
    } 

}; 

HTML

<form name="userForm" ng-submit="vm.submitForm()" novalidate> 

     <!-- NAME --> 
     <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid && !userForm.name.$pristine }"> 
      <label>Name</label> 
      <input type="text" name="name" class="form-control" ng-model="user.name" required> 
      <p ng-show="userForm.name.$invalid && !userForm.name.$pristine" class="help-block">You name is required.</p> 
     </div> 

     <!-- USERNAME --> 
     <div class="form-group" ng-class="{ 'has-error' : userForm.username.$invalid && !userForm.username.$pristine }"> 
      <label>Username</label> 
      <input type="text" name="username" class="form-control" ng-model="user.username" ng-minlength="3" ng-maxlength="8"> 
      <p ng-show="userForm.username.$error.minlength" class="help-block">Username is too short.</p> 
      <p ng-show="userForm.username.$error.maxlength" class="help-block">Username is too long.</p> 
     </div> 

     <!-- EMAIL --> 
     <div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine }"> 
      <label>Email</label> 
      <input type="email" name="email" class="form-control" ng-model="user.email"> 
      <p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p> 
     </div> 

     <button type="submit" class="btn btn-primary" ng-disabled="userForm.$invalid">Submit</button> 

</form> 

答えて

0

我々が行うことができます方法は次のとおりです。

は、ここに私のコードです。 これは、たとえばフォームの検証のみです。しかし、あなたがこれを行う前に、我々はkarma-ng-html2js-preprocessor

var scope, htmlForm; 

beforeEach(function() { 
    module('module'); 

}); 

beforeEach(inject($rootScope, $controller, $templateCache, $compile) { 
    scope = $rootScope.$new() 

    templateHtml = $templateCache.get('sample/sample.html') 
    formElement = angular.element("<div>" + templateHtml + "</div>") 
    $compile(formElement)(scope) 
    htmlForm= scope.htmlForm 

    scope.$apply() 
} 

it('should not allow an invalid `width`', function() { 
    expect(htmlForm.$valid).toBeTruthy(); 
    htmlForm.number.$setViewValue('LORA'); 
    expect(htmlForm.number.$valid).toBeFalsy() 
}); 
+0

を設定する必要がありますがplunkerを更新することができる場合、私のプロジェクトでこれを実装する方法、それが参考になるかわかりません。 – ankitd