javascript - 如何使用 jasmine 对 Angular 形式验证进行单元测试

标签 javascript angularjs unit-testing jasmine karma-jasmine

我创建了一个基本页面,用于验证输入。

如何对表单验证进行单元测试?

这是我的代码:

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>

最佳答案

我们可以采取以下方式。 这仅是表单验证的示例。但在执行此操作之前,我们需要配置 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()
});

关于javascript - 如何使用 jasmine 对 Angular 形式验证进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40019907/

相关文章:

c# - 调试单元测试时,Visual Studio c# 不应启动 IIS 网站

unit-testing - angular 2 rc7 - 测试服务 - 错误 : No provider for String

javascript - 如何在 Nuxt 中使用一个组件创建无限路由

javascript - 在 HighCharts 中如何自动设置 xAxis 的数量?

javascript - 使用 NG Repeat 继续打印数组

javascript - Angular.js 中的 Promise

unit-testing - 如何在F#中保持方法返回类型 'void'?

javascript - Angular ui-router - 如何访问从父模板传递的嵌套命名 View 中的参数?

javascript - 如何将不属于标签的文本包装在标签中?

angularjs - Angular 2通过服务器API加载数据: data.切片错误