【问题标题】:How to test form validation in Angular如何在 Angular 中测试表单验证
【发布时间】:2013-12-11 00:50:18
【问题描述】:

我正在尝试围绕使用 Angular 的表单编写测试。

在关注this solution 之后,我可以在 e2e 测试中访问表单的范围。现在使用此代码:

scope('Form', function(scope) {
    scope.email = "test@test.com";
    scope.password = "abcd1234";

    expect(scope.form.$valid).toBe(true);
})

无论出于何种原因,scope.form.$valid 都是错误的。如果我将它包裹在 setTimeout() 中,它会非常好用。 Angular 的sleep() 方法没用。

任何指针?

【问题讨论】:

    标签: validation angularjs testing jasmine end-to-end


    【解决方案1】:

    你应该$digest在最后一个scope.password之后添加scope.$digest()的范围。

    【讨论】:

    • 抱歉,还需要执行几个步骤才能使其正常工作,但您是对的。谢谢!
    【解决方案2】:

    多亏了 mimo 的建议,我得出了最终的解决方案。

    必须构造一个自定义 dsl 语句,以欺骗 expect 接受 Future。

    angular.scenario.dsl('expectScope', function() {
      var _retrieve = function(source, target) {
          if(target == '') return source;
          var targets = target.split('.');
          var nextTarget = targets[0];
          return _retrieve(source[nextTarget], targets.splice(1).join('.') );
      };
      var chain = angular.extend({}, angular.scenario.matcher);
    
      chain.not = function() {
          this.inverse = true;
          return chain;
      };
    
      return function(scope, name) {
          this.future = new angular.scenario.Future('scope.'+name, function() {});
          this.future.value = _retrieve(scope, name);
          return chain;
      };
    });
    

    通过在expectScope(scope, 'value.othervalue') 之前调用scope.$digest,现在一切正常。

    【讨论】:

      猜你喜欢
      • 2016-03-13
      • 2019-12-18
      • 1970-01-01
      • 2016-09-06
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      相关资源
      最近更新 更多